Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple value enum in Obj-C

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?

like image 750
gcamp Avatar asked Nov 14 '10 04:11

gcamp


People also ask

Can enum store multiple values?

we should do the following steps to have an enum with different values: Create enum constructor which accepts multiple values. Assign each constructor argument to a member field in the enum definition. Create getter methods so we can access any of the values assigned to a particular enum constant.

How do you declare an enum in Objective C?

Enums are defined by the following the syntax above. typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA, MyEnumValueB, MyEnumValueC, }; You also can set your own raw-values to the enumeration types. typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, };

Can enum have two same values?

1. Two enum names can have same value. For example, in the following C program both 'Failed' and 'Freezed' have same value 0.

Can enum have float values in C?

Enums can only be ints, not floats in C# and presumably unityScript.


2 Answers

<< 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"

like image 77
Joel Avatar answered Oct 03 '22 08:10

Joel


<< 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.

like image 32
Yann Ramin Avatar answered Oct 03 '22 07:10

Yann Ramin