我已经看到这在其他语言中是可能的,但在objective-c中需要这样的东西
我有一个类似的枚举
typedef enum {
option1 = 1 << 0,
option2 = 1 << 1,
option3 = 1 << 2
...
...
} SomePossibleOptions;
然后用户可以创建所需选项的掩码
SomePossibleOptions myOptions = option1 | option2;
[self.someObject performOperationsForOptions:myOptions];
-(void)performOperationsForOptions:(SomePossibleOptions)theOptions
{
if (myOptions & option1)
{
// do something
}
if (myOptions & option2
{
// do something
}
//(could use a switch statement)
}
但更愿意使用某种语法
foreach (option in myoption)
{
//do something
}