总结一下,新的属性绑定规则以下: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;