将NS_OPTIONS存储在swift词典中

时间:2015-07-19 11:14:15

标签: ios objective-c swift

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

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

所以在objc中我可以通过将整数值与@( )

一起装箱来实现
NSDictionary *dict = @{ @"s": @(RKRequestMethodGET) }

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

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

1 个答案:

答案 0 :(得分:1)

我确实搞清楚了。它实际上很简单:

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