ios - 将 NS_OPTIONS 存储在 swift 字典中

标签 ios objective-c swift

我在 objc 中定义了以下枚举:

typedef NS_OPTIONS(NSInteger, RKRequestMethod) {
    RKRequestMethodGET          = 1 << 0,
    RKRequestMethodPOST         = 1 << 1,
    // ...
};

因此,在 objc 中,我可以通过使用 @( ) 将整数值装箱来做到这一点:

NSDictionary *dict = @{ @"s": @(RKRequestMethodGET) }

现在在 swift 中,我想将这样的枚举存储在字典中:

var v = [String: AnyObject]()
v = ["s": RKRequestMethod.POST
v = ["s": NSNumber(char: RKRequestMethod.POST)]
v = ["s": NSNumber(unsignedChar: RKRequestMethod.POST)]
v = ["s": NSNumber(short: RKRequestMethod.POST)]
v = ["s": NSNumber(unsignedShort: RKRequestMethod.POST)]
// There are approx 10 more of these and I tried them all

最佳答案

我确实想通了。其实很简单:

v = ["s": RKRequestMethod.Any.rawValue]

关于ios - 将 NS_OPTIONS 存储在 swift 字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31500568/

相关文章:

swift - 在 Swift 中保存 UITableViewCell 复选标记选择

swift - 如何在子类中使用新的 collectionView?

javascript - Cardova IOS 10. "Failed to load webpage with error: The URL can’ t被显示”

ios - 为什么 xcode 无法加载我的 NIB

ios - 无法滑动图片

iphone - NSDateComponents包装在结构中?

ios - iMessage 扩展中未调用 textView() 方法

iphone - 在 UISplitViewController 中重用详细 View Controller

ios - 获取 Today-Extension 中通知中心的宽度

ios - 删除 MKMapView 注解会导致泄漏