Objective-C:检查是否使用枚举选项 [英] Objective-C: Check if using enum option
本文介绍了Objective-C:检查是否使用枚举选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用typedef枚举的自定义对象。如果我为我的对象设置了一些枚举选项,我该如何检查它们是否被使用?
I have a custom object that is using a typedef enum. If I set a few of the enum options for my object, how can I check to see if those are being used?
typedef enum {
Option1,
Option2,
Option3
} Options;
创建我的对象时我可能会使用:
When creating my object I might use:
myobject.options = Option1 | Option2;
我如何稍后检查哪些枚举选项已设置?如:
How can I then later check which enum options were set? Such as:
if (myobject.options == Option1) {
// Do something
}
if (myobject.options == Option2) {
// Do something
}
推荐答案
如果要为options参数执行按位逻辑,则应定义枚举,以便每个选项只设置一个位:
If you want to do bitwise logic for your options parameter, then you should define your enum so that each option only has a single bit set:
typedef enum {
Option1 = 1, // 00000001
Option2 = 1 << 1, // 00000010
Option3 = 1 << 2 // 00000100
} Options;
然后使用按位OR运算符设置选项:
Then you set your options using the bitwise OR operator:
myObject.options = Option1 | Option2;
并检查使用按位AND运算符设置的选项:
and check which options have been set using the bitwise AND operator:
if(myObject.options & Option1) {
// Do something
}
这篇关于Objective-C:检查是否使用枚举选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文