2010-08-02 61 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