你好,游客 登录 注册 搜索
背景:
阅读新闻

iOS6下Objective-C最新特性

[日期:2013-03-15] 来源:Linux社区  作者:mygamezone [字体:大 中 小]

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

linux
相关资讯       Objective-C 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款