總結一下,新的屬性綁定規則以下:ios
● 除非開發者在實現文件中提供getter或setter,不然將自動生成數組
● 除非開發者同時提供getter和setter,不然將自動生成實例變量xcode
● 只要寫了synthesis,不管有沒有跟實例變量名,都將生成實例變量.net
● 如開發者寫了@synthesize foo;那麼實例變量名就是foocode
● dynamic優先級高於synthesisorm
● 對於寫了@dynamic的實現,全部的對應的synthesis都將不生效blog
@literals(簡寫)ip
在xcode4.4之前ci
NSNumberelement
全部的[NSNumber numberWith…:]方法均可以簡寫了:
● [NSNumber numberWithChar:‘X’]簡寫爲 @‘X’;
● [NSNumber numberWithInt:12345] 簡寫爲 @12345
● [NSNumber numberWithUnsignedLong:12345ul] 簡寫爲 @12345ul
● [NSNumber numberWithLongLong:12345ll] 簡寫爲 @12345ll
● [NSNumber numberWithFloat:123.45f] 簡寫爲 @123.45f
● [NSNumber numberWithDouble:123.45] 簡寫爲 @123.45
● [NSNumber numberWithBool:YES] 簡寫爲 @YES
NSDictionary
● [NSDictionary dictionary] 簡寫爲 @{}
● [NSDictionary dictionaryWithObject:o1forKey:k1] 簡寫爲 @{ k1 : o1 }
● [NSDictionarydictionaryWithObjectsAndKeys:o1, k1, o2, k2, o3, k3, nil] 簡寫爲 @{ k1 : o1, k2 : o2, k3 : o3 }
當寫下@{ k1 : o1, k2 : o2, k3 : o3 }時,實際的代碼會是
// compiler generates:
id objects[] = { o1, o2, o3 };
id keys[] = { k1, k2, k3 };
NSUInteger count = sizeof(objects) / sizeof(id);
dict = [NSDictionary dictionaryWithObjects:objects forKeys:keyscount:count];
NSArray
部分NSArray方法獲得了簡化:
● [NSArray array] 簡寫爲 @[]
● [NSArray arrayWithObject:a] 簡寫爲 @[ a ]
● [NSArray arrayWithObjects:a, b, c, nil] 簡寫爲 @[ a, b, c ]
好比對於@[ a, b, c ],實際編譯時的代碼是
// compiler generates:
id objects[] = { a, b, c };
NSUInteger count = sizeof(objects)/ sizeof(id);
array = [NSArray arrayWithObjects:objectscount:count];
Mutable版本和靜態版本 上面所生成的版本都是不可變的,想獲得可變版本的話,能夠對其發送-mutableCopy消息以生成一份可變的拷貝。好比
NSMutableArray *mutablePlanets = [@[ @"Mercury", @"Venus", @"Earth", @"Mars", @"Jupiter", @"Saturn", @"Uranus", @"Neptune" ] mutableCopy];
另外,對於標記爲static的數組,不能使用簡寫爲其賦值(其實原來的傳統寫法也不行)。
若是直接賦值就會提示出錯
@implementation MyClass static NSArray * thePlanets = @[ error:array literals not constant @"Mercury", @"Venus", @"Earth", @"Mars", @"Jupiter", @"Saturn", @"Uranus", @"Neptune" ];解決方法是在類方法+ (void)initialize中對static進行賦值。
@implementation MyClass static NSArray *thePlanets; + (void)initialize{ if (self == [MyClass class]) { thePlanets = @[ @"Mercury", @"Venus", @"Earth", @"Mars", @"Jupiter", @"Saturn", @"Uranus", @"Neptune" ]; } }下標
Array
Song *oldSong = [_songs objectAtIndex:idx]; [_songs replaceObjectAtIndex:idx withObject:newSong]; 能夠簡寫爲 Song *oldSong = _songs[idx]; _songs[idx] = newSong; Dictionary id oldObject = [_storage objectForKey:key]; [_storage setObject:newobject forKey:key]; 能夠簡寫爲 id oldObject = _storage[key]; _storage[key] = newObject; 並且你不單單能使用它所提供的下標訪問。你也能夠對自定義的類使用下標訪問。 對於咱們自定義的類,只須要實現一下的方法就能使用下標訪問。 Array - (elementType)objectAtIndexedSubscript:(indexType)idx; - (void)setObject:(elementType)object atIndexedSubscript:(indexType)idx; Dictionary - (elementType)objectForKeyedSubscript:(keyType)key; - (void)setObject:(elementType)object forKeyedSubscript:(keyType)key;