iOS: About enumerations

Source: Internet
Author: User

In Computer science theory, an enumeration is a set of named integer constants. The defined form is as follows:

Enum enum-name {              identifier [= integer constant],              identifier [= integer constant],              ...              identifier [= integral type constant],         
In memory space, an enum is an enum-type union, which is a common body and members share a variable buffer. It does not participate in memory occupancy and deallocation, and the variables defined by the enumeration can be used directly or even without initialization.
If the enumeration is not initialized, it starts with the first identifier, which is assigned to the identifier 0, 1, 2, .... The following example enumerates the N1,N2,N3,N4, respectively, as 0,1,2,3
Enum
However, when a member in the enumeration is assigned a value, the subsequent member determines its value by a rule that adds 1 in turn.
enum nsinteger{    N1,    0,        
In the above enumeration example, N1,N2,N3,M4 is 0,0,10,11 respectively.
1. Each member (identifier) Terminator in the enumeration is ",", not ";", and the last member can omit ",".

3. Enumeration variables can only take an identifier constant in the enumeration description structure, such as n4=11 at this time.

Although an enumeration type is defined as an integral collection, I can test or define it as a string, declared as follows:
typedefenum: Nsuinteger {blackcolor, Redcolor, yellowcolor} threecolor;
But the usage is somewhat different from an integer enumeration, it cannot be used directly as an integral type, the direct output is always 0, and a method needs to be written to convert the string you need.
-(nsstring*) Colorconverttostring: (Threecolor) acolor {nsstring*result =Nil; Switch(acolor) { CaseBlackcolor:result=@"Black";  Break;  CaseRedcolor:result=@"Red";  Break;  CaseYellowcolor:result=@"Yellow";  Break; default: Result=@"Unknown"; }        returnresult;} Print Result: NSLog (@"%@", [self colorconverttostring:redcolor]); Red
But always feel that string enumeration has no meaning and has never been used.

Enumerations also support bitwise operations, such as those found in the UIView class.
typedef ns_options (Nsuinteger, uiviewautoresizing) {Uiviewautoresizingnone=0, Uiviewautoresizingflexibleleftmargin=1<<0, Uiviewautoresizingflexiblewidth=1<<1, Uiviewautoresizingflexiblerightmargin=1<<2, Uiviewautoresizingflexibletopmargin=1<<3, Uiviewautoresizingflexibleheight=1<<4, Uiviewautoresizingflexiblebottommargin=1<<5};
1<<0,0 means no shift 1 or 1; More than 1 will shift so the decimal value is
typedef ns_options (Nsuinteger, uiviewautoresizing) {Uiviewautoresizingnone=0,//Decimal, specified number of digits, binaryUiviewautoresizingflexibleleftmargin =1<<0,//1,0,01Uiviewautoresizingflexiblewidth =1<<1,//2,1,10 Convert to decimal 2Uiviewautoresizingflexiblerightmargin =1<<2,//4,2,100 Convert to decimal 4Uiviewautoresizingflexibletopmargin =1<<3,//8,3,1000 Convert to decimal 8Uiviewautoresizingflexibleheight =1<<4,//16,4,10000 converted to decimalUiviewautoresizingflexiblebottommargin =1<<5      //32,5,100000 converted into decimal};
The bitwise operation is computed by converting the binary to decimal, that is, the computed decimal value is accessed inside the enumeration value. But note that string enumeration does not apply to bitwise operations.

So it doesn't make any difference in the code to write the following.
    [UIButton Buttonwithtype:uibuttontypecustom];    [UIButton Buttonwithtype:0];
The enumeration can also be written in this way: (UIApplication.h)
typedef ns_options (Nsuinteger, uiinterfaceorientationmask) {uiinterfaceorientationmaskportrait= (1<<uiinterfaceorientationportrait), Uiinterfaceorientationmasklandscapeleft= (1<<uiinterfaceorientationlandscapeleft), Uiinterfaceorientationmasklandscaperight= (1<<uiinterfaceorientationlandscaperight), Uiinterfaceorientationmaskportraitupsidedown= (1<<uiinterfaceorientationportraitupsidedown), Uiinterfaceorientationmasklandscape= (Uiinterfaceorientationmasklandscapeleft |uiinterfaceorientationmasklandscaperight), Uiinterfaceorientationmaskall= (Uiinterfaceorientationmaskportrait | Uiinterfaceorientationmasklandscapeleft | Uiinterfaceorientationmasklandscaperight |uiinterfaceorientationmaskportraitupsidedown), Uiinterfaceorientationmaskallbutupsidedown= (Uiinterfaceorientationmaskportrait | Uiinterfaceorientationmasklandscapeleft |uiinterfaceorientationmasklandscaperight),};
Sometimes, it is often said to enumerate an array, where the enumeration is actually the meaning of traversal. An enumerated class is provided for us in iOS. (NSEnumerator.h)
  @protocol   nsfastenumeration -( Nsuinteger) Countbyenumeratingwithstate: (Nsfastenumerationstate *) state objects: (id   __unsafe_unretained []) buffer count: (Nsuinteger) len;   @end   @interface  Nsenumerator:nsobject <nsfastenumeration>-(id    @end   @interface   Nsenumerator (nsextendedenumerator) @property ( readonly , copy) Nsarray *allobjects;   @end  
collection classes (such as: Nsarray, Nsset, Nsdictionary, and so on) can all be obtained to Nsenumerator, which is an abstract class that does not have a public interface to create an instance. The Nextobject method of the Nsenumerator can traverse each set element, ending with nil and iterating through all the items in the collection by using with while.
Example:
//  ... ; Nsenumerator *enumerator = [Anarray objectenumerator]; ID Object ;  while ((object = [Enumerator nextobject]) {    // do something with object...}
Traversing an object at each index of an array, you can write a 0 to [array count] loop, and Nsenumerator is used to describe how this set iterates.
The Reverseobjectenumerator method is used to request an enumerator through objectenumerator, and if you want to browse the collection from behind. After you get the enumerator, you can start a while loop, and each time the loop requests its next object to the enumerator: Nextobject. Nextobject returns the nil value, the loop ends.
You can also customize the enumerator, you must implement the Nsfastenumeration protocol to implement a fast enumeration, that is,
-(Nsuinteger) Countbyenumeratingwithstate: (Nsfastenumerationstate *) state objects: (ID *) stackbuf count: (Nsuinteger) Len
and covers the nextobject method.

iOS: About enumerations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.