1.多用字面量语法,少用与之等价的方法
1.字面量字符串的创建
NSString *str =@"I Love You!";
NSString *str = [[NSString alloc] init....]
上边的一种就是字面量
2.字面数值
NSNumber *intNum = [NSNumbernumberWithInt:1];
NSNumber *floatNum = [NSNumbernumberWithFloat:2.0f];
NSNumber *intNum1 =@1;
NSNumber *floatNum1 =@2.0f;
3.字面数组
NSArray *arr = [NSArrayarrayWithObjects:@"1",@"2",@"3",nil];
//拿数组中的第一个元素
NSString *obj = [arr objectAtIndex:0];
NSArray *arr1 =@[@"1",@"2",@"3"];
//拿数组中的第一个元素
NSString *obj1 = arr1[0];
下边的是字面数组
4.字面字典
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:@"I",@"1",@"Love",@"2",@"You",@"3",nil];
NSString *value1 = [dicobjectForKey:@"1"];
NSDictionary *dic1 =@{@"1":@"I",@"2":@"Love",@"3":@"You"};
NSString *value2 = dic1[@"2"];
下边的是字面字典
5.可变数组和字典
通过字面量的取下标操作
NSArray *arrTest =@[@"1",@"2",@"3"];
NSMutableArray *mutableArr = [NSMutableArrayarrayWithArray:arrTest];
NSDictionary *dicTest =@{@"1":@"I",@"2":@"Love"};
NSMutableDictionary *mutableDic = [NSMutableDictionarydictionaryWithDictionary:dicTest];
[mutableArr replaceObjectAtIndex:0withObject:@"100"];
mutableArr[0] =@"100";
[mutableDic setObject:@"You"forKey:@"1"];
mutableDic[@"1"] =@"You";
2.多用类型常量,少用宏定义(这个我之前细说过这个问题)
今天提到另一点:在头文件中extern一个语句,例如:
externconst int itemHeight;
constint itemHeight = 100;
3.属性中的atomatic与nonatomaic
atomatic与nonatomatic区别:
具备atomaic特质的获取方法会通过锁定机制来确保其操作的原子性。也就是说,如果两个线程读写同一属性,那么不论何时,总能看到有效的属性值,如果不加锁(也就是使用nonatomatic),那么当其中一个线程正在改写某属性值时,另外一个线程也许会突然闯入,把还没修改好的属性值读取出来。发生这种情况,线程读到的属性值可能就不对了。这么一听,对啊,那赶紧用atomaic啊,用什么nonatomaic?