标签:
Objective-c早就支持字面量,但是IOS到XCODE 4.5,IOS6,LLVM4.0才开始较好的支持字面量。
以下是简要对比:
未使用字面量 |
使用字面量 |
---|---|
NSString *greeting = [NSString stringWithUTF8String:"Hello World"]; | NSString *greeting = @"Hello World"; |
NSNumber *number; | NSNumber *number; |
number = [NSNumber numberWithChar:‘X‘]; | number = @‘X‘; |
number = [NSNumber numberWithInt:12345]; | number = @12345; |
number = [NSNumber numberWithUnsignedLong:12345ul]; | number = @12345ul; |
number = [NSNumber numberWithLongLong:12345ll]; | number = @12345ll; |
number = [NSNumber numberWithFloat:123.45f]; | number = @123.45f; |
number = [NSNumber numberWithDouble:123.45]; | number = @123.45; |
number = [NSNumber numberWithBool:YES]; | number = @YES; |
[NSNumber numberWithint:6 + x * 2012] | @(6+x*2012) |
NSNumber *piOverSixteen = [NSNumber numberWithDouble: (M_PI / 16)]; | NSNumber *piOverSixteen = @( M_PI / 16 ); |
NSNumber *hexDigit = [NSNumber numberWithChar:"0123456789ABCDEF"[i % 16]];NSNumber *hexDigit = @( "0123456789ABCDEF"[i % 16] ); | NSNumber *hexDigit = @( "0123456789ABCDEF"[i % 16] ); |
NSNumber *usesScreenFonts = [NSNumber numberWithBool:[NSLayoutManager usesScreenFonts]]; | NSNumber *usesScreenFonts = @( [NSLayoutManager usesScreenFonts] ); |
NSNumber *writingDirection = [NSNumber numberWithInt:NSWritingDirectionLeftToRight]; | NSNumber *writingDirection = @( NSWritingDirectionLeftToRight ); |
NSNumber *path = [NSString stringWithUTF8String: getenv("PATH")]; | NSNumber *path = @( getenv("PATH") ); |
array = [NSArray arrayWithObjects:a, b, c, nil]; | array = @[ a, b, c ]; |
[array objectAtIndex:1] | array[1] |
dict = [NSDictionary dictionaryWithObjects:@[o1, o2, o3] forKeys:@[k1, k2, k3]]; | dict = @{ k1 : o1, k2 : o2, k3 : o3 }; |
[NSDictionary dictionaryWithObjectsAndKeys: v1, k1, v2, k2, nil]; | @{k1:v1, k2:v2} |
[dictionary valueForKey:k] | dictionary[k] |
注:
@YES/@NO和下标功能从IOS6才开始支持(XCODE4.5+LLVM4.0)。
其他字面量从XCODE4.4+LLVM4.0就已经支持。
标签:
原文地址:http://www.cnblogs.com/keyan1102/p/4545727.html