Obj-C 中的多值枚举

发布于 2024-10-02 07:21:07 字数 789 浏览 4 评论 0 原文

在 Cocoa 和 Cocoa Touch 框架中,枚举用作常量。我了解如何使用它,但在一种情况下,您可以使用 | 运算符将多个值作为参数传递。就像:

pageControl.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin);

枚举是这样声明的:

enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

我如何定义自己这种类型的枚举(即 << 的含义)以及如何在作为参数传递时检查多个值?

In the Cocoa and Cocoa Touch frameworks, enums are used as constant. I understand how to use it except in one case, the case you can pass as a parameter multiple value with the | operator. Like in :

pageControl.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin);

The enum is declared like that:

enum {
    UIViewAutoresizingNone                 = 0,
    UIViewAutoresizingFlexibleLeftMargin   = 1 << 0,
    UIViewAutoresizingFlexibleWidth        = 1 << 1,
    UIViewAutoresizingFlexibleRightMargin  = 1 << 2,
    UIViewAutoresizingFlexibleTopMargin    = 1 << 3,
    UIViewAutoresizingFlexibleHeight       = 1 << 4,
    UIViewAutoresizingFlexibleBottomMargin = 1 << 5
};
typedef NSUInteger UIViewAutoresizing;

How can I define myself this type of enum (i.e. what << means) and how can I check for multiples values when passed as a parameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小…楫夜泊 2024-10-09 07:21:07

<< 是位移运算符。所以 1 << 2 告诉它将该位移动两个空格。

示例:

在二进制中,数字 1 为:

0001

1 << 2 表示将所有位向左移动 2 个空格,结果是这个值:

0100

4

因此,示例中每个 ENUM 的值是 1、2、4、8、16 等。他们也可以将每个枚举设置为这些值。但由于他们使用该枚举来表示多个值,因此二进制值使其更加清晰:

0001
0010
0100
1000

因此他们使用位移位进行编写。

因此,如果 I OR (|) 其中两个值在一起,例如 FlexibleLeftMargin (0001) 和 FlexibleWidth (0010) >),我会得到以下值:

0011

因此他们使用每个位作为标志,这样他们就知道您设置了多个值。

现在,您可以使用 AND 运算符 & 来确定是否设置了特定值。

0010 & 0011 = 0010

所以你可以这样做来检查你是否设置了一个枚举:

myenum = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin);
if((myenum & UIViewAutoresizingFlexibleLeftMargin) == UIViewAutoresizingFlexibleLeftMargin) {
  // myenum has UIViewAutoresizingFlexibleLeftMargin set!
}

希望这是有道理的。
有关按位运算的更详尽解释,请阅读:Wikipedia ~ 位运算符 或搜索围绕“位运算符

<< is the bitshift operator. So 1 << 2 tells it to shift the bit two spaces over.

Example:

In binary the number 1 is:

0001

1 << 2 means to shift all the bits to the left 2 spaces, which results in this value:

0100

or 4.

So the values of each ENUM in your example is, 1, 2, 4, 8, 16, etc. They could have just as well set each enum to those values. But since they use that enum for multiple values, the binary values makes it more clear:

0001
0010
0100
1000

so they wrote using the bit shifts.

so if I OR (|) two of those values together, for example FlexibleLeftMargin (0001) and FlexibleWidth (0010), I would get the following value:

0011

So they use each bit as a flag so they know you have multiple values set.

You can now use the AND operator & to figure out if you have a specific value set.

0010 & 0011 = 0010

So you could do this to check if you have one of enums set:

myenum = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin);
if((myenum & UIViewAutoresizingFlexibleLeftMargin) == UIViewAutoresizingFlexibleLeftMargin) {
  // myenum has UIViewAutoresizingFlexibleLeftMargin set!
}

Hopefully this makes sense.
For a more thurough explanation on bitwise operations read this: Wikipedia ~ Bit Operators or search around for "bit operators"

稳稳的幸福 2024-10-09 07:21:07

<<是左移运算符,意思是将左值向左移动N位。在本例中,它在枚举中设置单个位(位 1、2、3、4、5),这允许按位 OR 运算符 (|) 组合值。

<< is the left shift operator, meaning move the left value N bits to the left. In this case, it is setting a single bit (bit 1, 2, 3, 4, 5) in the enum, which allows the bitwise OR operator (|) to combine values.

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