Objective-C枚举,NS_ENUM& NS_OPTIONS [英] Objective-C Enumeration, NS_ENUM & NS_OPTIONS
本文介绍了Objective-C枚举,NS_ENUM& NS_OPTIONS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Objective-C中创建具有特定类型的枚举的正确方法是什么? NS_ENUM和NS_OPTIONS如何工作? NS_OPTIONS用于掩码,如NSAutoresizing?谢谢。
What's the correct way to create an enumeration with a specific type in Objective-C? How does NS_ENUM and NS_OPTIONS work? NS_OPTIONS are used for masks, like NSAutoresizing? Thanks.
Code from NSObjCRuntime.h
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#define NS_OPTIONS(_type, _name) _type _name; enum : _type
推荐答案
示例从 NSHipster 。 NS_OPTIONS以类似的方式使用,但是对于通常是有点掩码的枚举来说,
example from NSHipster. NS_OPTIONS is used in a similar way, but for enums which would ordinarily be a bit mask
而不是
typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;
或
typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};
typedef NSInteger UITableViewCellStyle;
执行此操作:
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
};
一个例子NS_OPTIONS枚举:
an example NS_OPTIONS enum:
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
这篇关于Objective-C枚举,NS_ENUM& NS_OPTIONS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文