NS_ENUM 和 NS_OPTIONS

2018-07-03  本文已影响35人  AprSnow

Enumeration Macros

在Apple的《Adopting Modern Objective-C》一文中提到用 NS_ENUMNS_OPTIONS 代替C语言风格的enum
比如,
普通枚举

enum {
        UITableViewCellStyleDefault,
        UITableViewCellStyleValue1,
        UITableViewCellStyleValue2,
        UITableViewCellStyleSubtitle
};
typedef NSInteger UITableViewCellStyle;

最好写为:

typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
        UITableViewCellStyleDefault,
        UITableViewCellStyleValue1,
        UITableViewCellStyleValue2,
        UITableViewCellStyleSubtitle
};

位枚举

enum {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
};
typedef NSUInteger UIViewAutoresizing;

最好写为:

typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
        UIViewAutoresizingNone                 = 0,
        UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
        UIViewAutoresizingFlexibleWidth        = 1 << 1,
};

Difference

More

Adopting Modern Objective-C

上一篇 下一篇

猜你喜欢

热点阅读