比较(目标)C 中的枚举数组
我想测试从一种状态到另一种状态的转换。我在这样的枚举中定义了我的状态:
enum FingerStatus {
FINGERS_UP,
MOVING,
FINGERS_STILL
};
我有一个“currentState”和一个“newState”变量。我知道枚举只是整数,如果它们是 16 位整数(我认为它们是),则可以将两个枚举表示为单个 32 位整数。
我觉得我应该能够按照
switch ({currentStatus, newFingerStatus}) {
case {FINGERS_STILL, MOVING}:
NSLog(@"fingers starting to move");
break;
case {MOVING, FINGERS_STILL}:
NSLog(@"fingers stopped moving");
break;
default:
break;
}
我意识到语法完全错误的方式做一些事情,但我认为基本想法是合理的。还有另一种好的、干净的方法来做到这一点吗?
I want to test for transition from one state to another. I have defined my states in an enum like this:
enum FingerStatus {
FINGERS_UP,
MOVING,
FINGERS_STILL
};
I have a "currentState" and a "newState" variable. I know that enums are just integers, and if they're 16-bit integers, which I think they are, it's possible to represent two enums as a single 32-bit integer.
I feel like I ought to be able to do something along the lines of
switch ({currentStatus, newFingerStatus}) {
case {FINGERS_STILL, MOVING}:
NSLog(@"fingers starting to move");
break;
case {MOVING, FINGERS_STILL}:
NSLog(@"fingers stopped moving");
break;
default:
break;
}
I realize the syntax is all wrong, but I think the basic idea is sound. Is there another nice, clean way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你能保证你的枚举小于 16 位,那么这将起作用:
If you can guarantee your enums are less than 16 bits, this will work:
由于开关只是一种编写 goto 的奇特方式,因此您不能使用它们来做您想做的事情。使用 if 代替:
Since switches are just a fancy way to write gotos, you can't use them to do what you want to do. Use ifs instead:
整数可以超过16位,在计算机上通常是32位(不确定iPhone),但你的想法仍然有效。
您可以使用 (intOne + ( intTwo << 4 )) 将两个 int 放在一起,进行比较和评估。
Ints can be more than 16bits, and usually 32bits on computers( not sure about iPhone), but your idea would still work.
You can use (intOne + ( intTwo << 4 )) to put the two ints together, for comparison and evaluation.
对于一个人来说,给枚举不同的唯一值可能是明智的:1,2,4而不是0,1,2,然后你可以按位或它们。然后你可以做这样的标签:
well for one it would probably wise to give the enums different unique values: 1,2,4 and not 0,1,2 then you can bitwise or them. Then you can do labels like: