2013-11-04 30 views
2

我試圖設置一個規則字典;鍵是字符串,我很樂意將這些值設置爲位圖。可以將NS_OPTION設置爲NSMutableDictionary中的值嗎?

我使用NS_OPTION申報項目這樣:

typedef NS_OPTIONS(NSInteger, PermittedDirection) { 
    LeftDirection = 1 << 0, 
    RightDirection = 1 << 1 
}; 

typedef NS_OPTIONS(NSInteger, PermittedSize) { 
    SmallSize = 1 << 0, 
    MediumSize = 1 << 1, 
    LargeSize = 1 << 2 
}; 

我有我的規則解釋這樣定義:

@property (atomic, strong) NSMutableDictionary * rules; 

後來我實例它是這樣:

self.rules = [[NSMutableDictionary alloc] init]; 

後來我嘗試添加位掩碼(如下所示)並得到一個錯誤,因爲枚舉不是指向對象的指針:

PermittedSize size = SmallSize | LargeSize; 
    [self.rules setObject:size forKey:ALLOWED_FISH_SIZE]; 

有沒有一種簡單的方法來包裝這些不知何故,而不會丟失在我檢索值時使用位掩碼的能力?

回答

3

您可以通過把它包裝上的NSNumber

PermittedSize size = SmallSize | LargeSize; 
self.rules[ALLOWED_FISH_SIZE] = @(size); 

然後,當你找回它,只是拆箱值:

PermittedSize size = (PermittedSize) [self.rules[ALLOWED_FISH_SIZE] integerValue]; 
+0

優秀的,謝謝! – marina

相關問題