WWDC2012发布了iOS6,同时为Objective C带来了一些新特性以简化编程。下面是这些新特性,需要XCode4.4及以上版本支持:
1.方法的申明顺序不再要求 在方法里面可以调用在后面申明的方法,编译器会帮助查找方法的申明,顺序不再要求。如下:
@interface SongPlayer : NSObject
- ( void ) playSong: ( Song * ) song;
@end
@implementation SongPlayer
- ( void ) playSong: ( Song * ) song {
NSError * error;
[ self startAudio:& error] ;//XCode4.4以前会提示方法未定义,XCode4.4以后可以放心使用
...
}
- ( void ) startAudio: ( NSError ** ) error { ... }
@end
2.枚举支持强类型 XCode4.4以前定义枚举使用如下方式,相当于定义了类型为int的枚举类型。
typedef enum {
NSNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle
} NSNumberFormatterStyle;
// typedef int NSNumberFormatterStyle;
XCode4.4以后可以为枚举指明强类型,这样在赋值时会有强类型限制(需要在Build Setting开启Suspicious implicit conversions)。定义如下:
typedef enum NSNumberFormatterStyle : NSUInteger {
NSNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle
} NSNumberFormatterStyle;
或使用NS_ENUM宏来定义
typedef NS_ENUM( NSUInteger, NSNumberFormatterStyle) {
NSNumberFormatterNoStyle,
NSNumberFormatterDecimalStyle,
NSNumberFormatterCurrencyStyle,
NSNumberFormatterPercentStyle,
NSNumberFormatterScientificStyle,
NSNumberFormatterSpellOutStyle
} ;
3.默认属性合成
@interface Person : NSObject
@property ( strong) NSString * name;
@end
@implementation Person {
NSString * _name;//这句可以省略,XCode很早就可以了
}
@synthesize name = _name;//XCode4.4以后,这句也可以省略,XCode默认合成带下划线的成员变量
@end
即可以简化为:
@interface Person : NSObject
@property ( strong) NSString * name;//ARC开启,否则需要自己release
@end
@implementation Person
@end
4.创建NSNumber的新语法 XCode4.4以前的创建方式:
NSNumber * value;
value = [ NSNumber numberWithChar: 'X' ] ;
value = [ NSNumber numberWithInt: 12345 ] ;
value = [ NSNumber numberWithUnsignedLong: 12345ul] ;
value = [ NSNumber numberWithLongLong: 12345ll] ;
value = [ NSNumber numberWithFloat: 123.45f] ;
value = [ NSNumber numberWithDouble: 123.45 ] ;
value = [ NSNumber numberWithBool: YES ] ;
XCode4.4以后可简化为:
NSNumber * value;
value = @'X' ;
value = @12345 ;
value = @12345ul;
value = @12345ll;
value = @123.45f;
value = @123.45 ;
value = @YES ;
XCode4.4以前,使用语句创建NSNumber:
XCode4.4以后可以通过”()”方式创建:
5.创建NSArray的新语法
NSArray * array;
array = @[ a, b, c ] ;
//相当于使用下面的方式创建:
id objects[ ] = { a, b, c } ;
NSUInteger count = sizeof ( objects) / sizeof ( id ) ;
array = [ NSArray arrayWithObjects: objects count: count] ;
6.创建NSDictionary的新语法
NSDictionary * dict;
dict = @{ } ;
dict = @{ k1 : o1 } ;
dict = @{ k1 : o1, k2 : o2, k3 : o3 } ;
//相当于如下方式:
id objects[ ] = { o1, o2, o3 } ;
id keys[ ] = { k1, k2, k3 } ;
NSUInteger count = sizeof ( objects) / sizeof ( id ) ;
dict = [ NSDictionary dictionaryWithObjects: objects
forKeys: keys
count: count] ;
7.mutable对象的创建,调用对象的-mutableCopy方法
NSMutableArray * mutablePlanets = [ @[
@ "Mercury" , @ "Venus" , @ "Earth" ,
@ "Mars" , @ "Jupiter" , @ "Saturn" ,
@ "Uranus" , @ "Neptune"
] mutableCopy] ;
8.静态容器对象的创建,使用+initialize方法
@implementation MyClass
static NSArray * thePlanets;
+ ( void ) initialize {
if ( self == [ MyClass class] ) {
thePlanets = @[
@ "Mercury" , @ "Venus" , @ "Earth" ,
@ "Mars" , @ "Jupiter" , @ "Saturn" ,
@ "Uranus" , @ "Neptune"
] ;
}
}
9.可变数组新的存取方式:
@implementation SongList {
NSMutableArray * _songs;
}
- ( Song * ) replaceSong: ( Song * ) newSong atIndex: ( NSUInteger) idx {
Song * oldSong = _songs[ idx] ;//使用[idx]访问子对象
_songs[ idx] = newSong;//使用[idx]设置子对象
return oldSong;
}
10.可变字典新的存取方式:
@implementation Database {
NSMutableDictionary * _storage;
}
- ( id ) replaceObject: ( id ) newObject forKey: ( id ) key {
id oldObject = _storage[ key] ;//相当于id oldObject = [_storage objectForKey:key];
_storage[ key] = newObject;//相当于[_storage setObject:object forKey:key];
return oldObject;
}