Objective C:检查启用标志的方法
我正在使用 bitflag 来启用/禁用代码中的某些功能。我有以下枚举。
typedef enum function {
function1 = 1 << 0,
function2 = 1 << 1,
function3 = 1 << 2,
function4 = 1 << 3
};
我知道我需要使用以下代码来检查哪个标志已启用。
if((flags & function1) == function1)
{
// do some action
}
在我的枚举中,标志的数量很大,为了检查每个标志,我需要进行很多“if”条件检查。有什么方法可以最大限度地减少所需的 if 语句的数量吗?我是目标 C 的新手,希望在目标 C 中实现它。提前致谢。
I am using bitflag to enable/disable some functionality in my code. I have following enum.
typedef enum function {
function1 = 1 << 0,
function2 = 1 << 1,
function3 = 1 << 2,
function4 = 1 << 3
};
I know that I need to use following code to check which flag has been enabled.
if((flags & function1) == function1)
{
// do some action
}
In my enum, number of flags is large and to check every flag, I need to have that many "if" condition checks. Is there any way which can minimize the number of if statements required? I am new to objective C and looking to implement it in objective C. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,通过按位或组合标志:
此外,要检查特定标志,您不需要
== function1
,flags & 部分。 function1
就足够了,因为它将计算为零或function1
。Yes, combine the flags via a bitwise or:
Moreover, to check for a particular flag you don't need the part
== function1
,flags & function1
is sufficient as it will evaluate either to zero orfunction1
.