我在 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/