NSString *name = @([self getMyName]);
數組可以這麼寫:
NSArray *array = @[@"1", @"2"];
NSString *str = array[0];
NSMutable *mutableArray = [@[@"1"] mutableCopy];
字典可以這麼寫:
NSDictionary *dict = @{@"name" : @"Jason", @"password" : @"Hello,world"};
NSString *name = dict[@"name"];
當寫下這一行程式
// when you write this:
dict = @{ k1 : o1, k2 : o2, k3 : o3 };
新的編譯器為我們做了些事情(代碼片段摘自WWDC 2012 Session 405):
// compiler generates:
id objects[] = { o1, o2, o3 };
id keys[] = { k1, k2, k3 };
NSUInteger count = sizeof(objects) / sizeof(id);
dict = [NSDictionarydictionaryWithObjects: objects
forKeys: keys
count: count];
沒有留言:
張貼留言