Objective-C枚举,NS_ENUM& NS_OPTIONS [英] Objective-C Enumeration, NS_ENUM & NS_OPTIONS

查看:121
本文介绍了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&amp; NS_OPTIONS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆