iOS特别知识点总结六

//判断内容是否全部为空格 yes 全部为空格 no 不是

- (BOOL)isEmpty:(NSString *) str {

    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];

        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];

        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

UITabBarController

//1.创建Window
 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 self.window.backgroundColor = [UIColor whiteColor];

 //a.初始化一个tabBar控制器
 UITabBarController *tb=[[UITabBarController alloc]init];
 //设置控制器为Window的根控制器
 self.window.rootViewController=tb;

 //b.创建子控制器
 UIViewController *c1=[[UIViewController alloc]init];
 c1.view.backgroundColor=[UIColor grayColor];
 c1.view.backgroundColor=[UIColor greenColor];
 c1.tabBarItem.title=@"消息";
 c1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
 c1.tabBarItem.badgeValue=@"123";

 UIViewController *c2=[[UIViewController alloc]init];
 c2.view.backgroundColor=[UIColor brownColor];
 c2.tabBarItem.title=@"联系人";
 c2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];

 UIViewController *c3=[[UIViewController alloc]init];
 c3.tabBarItem.title=@"动态";
 c3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];

 UIViewController *c4=[[UIViewController alloc]init];
 c4.tabBarItem.title=@"设置";
 c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"]; 

_weak typeof(self) weakSelf = self;

内存管理原则
1、默认strong,可选weak。strong下不管成员变量还是property,每次使用指针指向一个对象,等于自动调用retain(), 并对旧对象调用release(),所以设为nil等于release。
2、只要某个对象被任一strong指针指向,那么它将不会被销毁,否则立即释放,不用等runloop结束。所有strong指针变量不需要在dealloc中手动设为nil,iOS会自动处理,debug可以看到全部被置为nil,最先声明的变量最后调用dealloc释放。

3、官方建议IBOutlet加上__weak,实际上不用加也会自动释放;

4、优先使用私有成员变量,除非需要公开属性才用property。

5、避免循环引用,否则手动设置nil释放。

6、block方法常用声明:@property (copy) void(^MyBlock)(void); 如果超出当前作用域之后仍然继续使用block,那么最好使用copy关键字,拷贝到堆区,防止栈区变量销毁。

7、创建block匿名函数之前一般需要对self进行weak化,否则造成循环引用无法释放controller:

weak MyController *weakSelf = self 或者 weak __typeof(self) weakSelf = self;
执行block方法体的时候也可以转换为强引用之后再使用:MyController* strongSelf = weakSelf; if (!strongSelf) { return; }