2010-08-02 56 views
6

我正在嘗試使用與NSView的setAutoresizingMask方法類似的格式創建一個方法。我想讓某人能夠指定在我的枚舉(NSHeightSizable | NSWidthSizable)中聲明的多個值,就像自動調整掩碼一樣。我怎樣才能做到這一點?使用多個NSUInteger枚舉作爲方法的參數

回答

19

首先,在標題宣告你們的旗幟:

enum 
{ 
    AZApple = (1 << 0), 
    AZBanana = (1 << 1), 
    AZClementine = (1 << 2), 
    AZDurian = (1 << 3) 
}; 

typedef NSUInteger AZFruitFlags; 

(1 << 0)通過對(1 << 3)的整數進出一個整數,你可以「掩蓋」代表單位。例如,假設NSUInteger是32位,並且有人已選擇兩個蘋果和榴蓮,則整數是這樣的:

0000 0000 0000 0000 0000 0000 0000 1001 
            | |- Apple bit 
            |---- Durian bit 

通常你的方法需要採取一個無符號整數的參數:

- (void) doSomethingWithFlags:(AZFruitFlags) flags 
{ 
    if (flags & AZApple) 
    { 
     // do something with apple 

     if (flags & AZClementine) 
     { 
      // this part only done if Apple AND Clementine chosen 
     } 
    } 

    if ((flags & AZBanana) || (flags & AZDurian)) 
    { 
     // do something if either Banana or Durian was provided 
    } 
} 
+0

非常感謝!真的有幫助。 – 2010-08-02 23:50:56