简体   繁体   中英

Store NS_OPTIONS in swift dictionary

I have the following enum defined in objc:

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

So in objc I can do this by boxing the integer value with @( ) :

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

Now in swift, I want to store such an enum in a dictionary:

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

I did figure it out. It is actually as simple as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM