iOS的枚舉寫法有很多種.
typedef enum
{
environmentDebug = 1, /< 枚舉议经,預(yù)發(fā)環(huán)境 */
environmentDaily, /< 枚舉,日常環(huán)境 */
environmentDailyTwo, /< 枚舉弃秆,日常二套環(huán)境 */
environmentRelease /< 枚舉拗慨,正式環(huán)境 */
}MtopEnvironment;
typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
UIViewAnimationTransitionNone,//默認(rèn)從0開始
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};
enum{
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;
蘋果對(duì)于NS_ENUM的宏的定義在Foundation.framework的NSObjCRuntime.h中:
#if (__cplusplus &&__cplusplus >= 201103L && (__has_extension(cxx_strong_enums) || __has_feature(objc_fixed_enum))) || (!__cplusplus && __has_feature(objc_fixed_enum))
#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type
#if (__cplusplus)
#define NS_OPTIONS(_type, _name) _type _name; enum : _type
#else
#define NS_OPTIONS(_type, _name) enum _name : _type _name; enum _name : _type
#endif
#else
#define NS_ENUM(_type, _name) _type _name; enum
#define NS_OPTIONS(_type, _name) _type _name; enum
#endif
將typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
展開得到:
typedef enum UIViewAnimationTransition : NSInteger UIViewAnimationTransition;
enum UIViewAnimationTransition : NSInteger {
從枚舉定義來看险掀,NS_ENUM和NS_OPTIONS本質(zhì)是一樣的蝉绷,僅僅從字面上來區(qū)分其用途叫倍。NS_ENUM是通用情況,NS_OPTIONS一般用來定義具有位移操作或特點(diǎn)的情況(bitmask)佩厚。
實(shí)際使用時(shí)姆钉,可以直接定義:
typedef enum : NSInteger {....} UIViewAnimationTransition;
等效于上述定義