Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / iOS6下Objective-C最新特性

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 { ... }
@end2.枚举支持强类型
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