本文介绍了Obj-C中的多值枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在Cocoa和Cocoa Touch框架中,使用枚举作为常量。我理解如何使用它,除了在一种情况下,您可以使用 | 运算符作为参数多值传递的情况。像在: pageControl.autoresizingMask =(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin); 枚举声明如下: 枚举{ UIViewAutoresizingNone = 0, UIViewAutoresizingFlexibleLeftMargin = 1< 0, UIViewAutoresizingFlexibleWidth = 1<< 1, UIViewAutoresizingFlexibleRightMargin = 1<< 2, UIViewAutoresizingFlexibleTopMargin = 1<< 3, UIViewAutoresizingFlexibleHeight = 1<< 4, UIViewAutoresizingFlexibleBottomMargin = 1<< 5 }; typedef NSUInteger UIViewAutoresizing; 如何定义自己这种类型的枚举(即什么<<< ; 表示),当作为参数传递时,如何检查倍数值?解决方案 code><< 是位移操作符。所以 1 告诉它将两个空格移位。 示例: 二进制数字 1 是: 0001 1< 2 意味着将所有位移动到左侧2个空格,这将导致以下值: 0100 或 4 。 所以你的例子中每个ENUM的值是1,2,4,8,16等。他们可以把每个枚举都设置成这些值。但是由于它们使用该枚举的多个值,所以二进制值使其更加清楚: 0001 0010 0100 1000 所以他们使用位移写了。 所以如果我的OR( | )两个值在一起,例如 FlexibleLeftMargin ( 0001 )和FlexibleWidth( 0010 ),我会得到以下值: 0011 所以他们使用每一位作为标志,以便他们知道您有多个值设置。 现在可以使用AND运算符& 确定你是否设置了一个特定的值。 0010& 0011 = 0010 所以你可以这样做来检查你是否有一个枚举集: myenum =(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin); if((myenum& UIViewAutoresizingFlexibleLeftMargin)== UIViewAutoresizingFlexibleLeftMargin){ // myenum有UIViewAutoresizingFlexibleLeftMargin设置! } 希望这是有道理的。 有关按位操作的更多说明,请阅读以下内容:维基百科〜位操作员或搜索位操作员 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? 解决方案 << is the bitshift operator. So 1 << 2 tells it to shift the bit two spaces over.Example:In binary the number 1 is:00011 << 2 means to shift all the bits to the left 2 spaces, which results in this value:0100or 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:0001001001001000so 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:0011So 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 = 0010So 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" 这篇关于Obj-C中的多值枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-06 03:40