Use of the Ios/objective-c Development enum (enum)

Source: Internet
Author: User
Tags bitwise

In the Objective-c language, Apple introduced two macros in iOS to redefine the enumeration type (that is, Ns_enum and ns_options), which is essentially the same as defining enumerated types, but in using Ns_enum for general enumerations, Ns_enumm is used for general enumerations, while Es_options is used for enumerations with shift operations.

Ns_enum Use Example

typedef ns_enum (Nsuinteger, Type) {

Typenone = 0,

TypeTime = 1,

TypeText = 2,

Typetimeandtext = 3

};

Ns_options Use Example

typedef ns_options (Nsuinteger, Test) {

Testa = 1 << 0,

TESTB = 1 << 1,

TESTC = 1 << 2,

TESTD = 1 << 3

};

Enumerations with shift operations are used for situations where the same enumeration variable can simultaneously assign multiple enumeration members. The rationale is to bitwise OR (|) The individual enumeration values, because the enumeration members of the shift operation can guarantee the uniqueness of the results after the bitwise OR (|) calculation, so each result is reversed to calculate whether there are several enumeration members bitwise OR ( |) and Into

To accommodate bitwise OR (|) Assign an enumeration member to the enumeration variable test TESTA,TESTB

Test test = Testa | TESTB; To use bitwise XOR or (^) to remove an enumeration member from the enumeration variable test testa

Test ^= Testa;

Note: If the enumeration variable test itself is not assigned a value of Testa, then using bitwise XOR or (^) will assign an enumeration member to the enumeration variable test multiple Testa

To use bitwise AND (&) to determine whether the enumeration variable test is assigned the enumeration member Testa as an example

if (Test & Testa) {

NSLog (@ "yes");

}else {

NSLog (@ "no");

}





Related Article

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.