<OC>OC新特征array literals

类似于java5提供的autoboxing功能。
以前的写法:
NSNumber * number = [NSNumber numberWithInt:1];
NSArray * array = [NSArray arrayWithObjects:@"one", @"two",nil];
NSDictionary * dict = [NSDictionarydictionaryWithObjectsAndKeys:@"value1", @"key1", @"value2",@"key2", nil];
利用ObjectLiterals简化后的写法:
NSNumber * number = @1;
NSArray * array = @[@"one", @"two"];
NSDictionary * dict = @{@”key1″:@”value1″, @”key2″:@”value2″};

具体的示例:
// 整数
NSNumber *fortyTwo =@42; // 等价于 [NSNumber numberWithInt:42]
NSNumber *fortyTwoUnsigned =@42U; // 等价于[NSNumber numberWithUnsignedInt:42U]
NSNumber *fortyTwoLong =@42L; // 等价于 [NSNumber numberWithLong:42L]
NSNumber *fortyTwoLongLong =@42LL; // 等价于 [NSNumbernumberWithLongLong:42LL]
// 浮点数
NSNumber *piFloat =@3.141592654F; // 等价于 [NSNumber numberWithFloat:3.141592654F]
NSNumber *piDouble =@3.1415926535; // 等价于 [NSNumbernumberWithDouble:3.1415926535]
// 布尔值
NSNumber *yesNumber =@YES; // 等价于 [NSNumber numberWithBool:YES]
NSNumber *noNumber =@NO; // 等价于 [NSNumber numberWithBool:NO]
// 空数组
NSArray * array =@[]; // 等价于 [NSArray array]
// 空的字典
NSDictionary * dict =@{}; // 等价于 [NSDictionary dictionary]

posted @ 2014-04-01 18:34  白条围巾  阅读(250)  评论(0编辑  收藏  举报