Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法(一)
NSNumber *number = [NSNumber numberWithInt:1]; //字面量写法: NSNumber *number = @1; NSNumber *doubleNumber = @3.14159; NSNumber *boolNumber = @yes; NSNumber *charNumber = @‘a’;
int x = 5; float y = 6.23f; NSNumber *expressNumber = @(x * y);
NSArray *animals = [NSArray arrayWithObjects:@“cat”, @“dog”, @“mouse”, @“badger”, nil]; // 使用字面量: NSArray *animals = @[@“cat”, @“dog”, @“mouse”, @“badger”];
NSArray *arr1 = [NSArray arrayWithObjects:object1, object2, object3, nil]; NSArray *arr2 = @[object1, object2, object3];
NSDictionary *person = [NSDictionary dictionaryWithObjectAndKeys:@“matt”,@“first name”,@“galloway”, @“lastName”,nil]; // 使用字面量: NSDictionary *person = @{@“first name”:@“matt”, @“lastName”:@“galloway”};
NSString *lastName = [person objectForKey:@“lastname”]; NSString *lastName = person[@“lastname”];
4. 【多用类型常量,少用#define预处理指令】
5. 【用枚举表示状态、选项、状态码】
typedef NS_ENUM(NSUInteger, EOCConnectionState) { EOCConnectionStateDisconnected, EOCConnectionStateDisconnected, EOCConnectionStateDisconnected }; typedef NS_OPTIONS(NSUInteger, EOCPermittedDirection) { EOCCPermittedDirectionUp = 1 << 0, EOCCPermittedDirectionDown = 1 << 1, EOCCPermittedDirectionLeft = 1 << 2, EOCCPermittedDirectionRight = 1 << 3, };
enum EOCConnectionState connectionState : NSInteger { // };
Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法(一),,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。