比较(目标)C 中的枚举数组

发布于 2024-09-25 12:49:58 字数 585 浏览 6 评论 0原文

我想测试从一种状态到另一种状态的转换。我在这样的枚举中定义了我的状态:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

二智少女 2024-10-02 12:49:58

如果你能保证你的枚举小于 16 位,那么这将起作用:

switch ((currentStatus<<16) + newFingerStatus) {
    case (FINGERS_STILL<<16) + MOVING:
        NSLog(@"fingers starting to move");
        break;
    case (MOVING<<16) + FINGERS_STILL:
        NSLog(@"fingers stopped moving");
        break;
    default:
        break;
}

If you can guarantee your enums are less than 16 bits, this will work:

switch ((currentStatus<<16) + newFingerStatus) {
    case (FINGERS_STILL<<16) + MOVING:
        NSLog(@"fingers starting to move");
        break;
    case (MOVING<<16) + FINGERS_STILL:
        NSLog(@"fingers stopped moving");
        break;
    default:
        break;
}
与他有关 2024-10-02 12:49:58

由于开关只是一种编写 goto 的奇特方式,因此您不能使用它们来做您想做的事情。使用 if 代替:

if(currentStatus == FINGERS_STILL && newFingerStatus == MOVING)
{
    NSLog(@"fingers starting to move");
}
else if(currentStatus == MOVING && newFingerStatus == FINGERS_STILL)
{
    NSLog(@"fingers stopped moving");
}

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:

if(currentStatus == FINGERS_STILL && newFingerStatus == MOVING)
{
    NSLog(@"fingers starting to move");
}
else if(currentStatus == MOVING && newFingerStatus == FINGERS_STILL)
{
    NSLog(@"fingers stopped moving");
}
や莫失莫忘 2024-10-02 12:49:58

整数可以超过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.

娇俏 2024-10-02 12:49:58

对于一个人来说,给枚举不同的唯一值可能是明智的:1,2,4而不是0,1,2,然后你可以按位或它们。然后你可以做这样的标签:

case FINGERS_STILL|MOVING:

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:

case FINGERS_STILL|MOVING:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文