IOS Enumeration type enum NS_ENUM NS_OPTIONS

Source: Internet
Author: User

IOS Enumeration type enum NS_ENUM NS_OPTIONS

Generally, we can use the C-style enum keyword to define the enumeration type.

Enum {
UIViewAnimationTransitionNone,
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
} UIViewAnimationTransition;

// Definition of the displacement operation Enumeration
Enum {
UIViewAutoresizingNone = 0,
UIViewAutoresizingFlexibleLeftMargin = 1 <0,
UIViewAutoresizingFlexibleWidth = 1 <1,
UIViewAutoresizingFlexibleRightMargin = 1 <2,
UIViewAutoresizingFlexibleTopMargin = 1 <3,
UIViewAutoresizingFlexibleHeight = 1 <4,
UIViewAutoresizingFlexibleBottomMargin = 1 <5
};
Typedef NSUInteger UIViewAutoresizing; // UIViewAutoresizing can be used where NSUInteger is used, // UIViewAutoresizing is equivalent to an alias of NSUInteger.
// Therefore, a variable of UIViewAutoresizing can be directly assigned to NSUInteger.
 

The enumerated value is generally a four-byte int value, which is 8 bytes in a 64-bit system.

After iOS6 and Mac OS 10.8, Apple introduced two macros to redefine the two enumeration types. In fact, the enum definition and typedef are combined into one, different macros are used to distinguish them from the code perspective.

NS_OPTIONS is generally used to define the enumerated values of displacement-related operations. We can refer to the header file of UIKit. Framework to see a large number of enumeration definitions.

Typedef NS_ENUM (NSInteger, UIViewAnimationTransition ){
UIViewAnimationTransitionNone, // The default value starts from 0.
UIViewAnimationTransitionFlipFromLeft,
UIViewAnimationTransitionFlipFromRight,
UIViewAnimationTransitionCurlUp,
UIViewAnimationTransitionCurlDown,
};

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
};

 

The two macros are defined in NSObjCRuntime. h of Foundation. framework:

 

  1. # If (_ cplusplus & _ cplusplus> = 201103L & (_ has_extension (cxx_strong_enums) | _ has_feature (objc_fixed_enum) | (! _ Cplusplus & _ has_feature (objc_fixed_enum) # define NS_ENUM (_ type, _ name) enum _ name: _ type _ name; enum _ name: _ type
  2. # If (_ cplusplus) # define NS_OPTIONS (_ type, _ name) _ type _ name; enum: _ type
  3. # Else # define NS_OPTIONS (_ type, _ name) enum _ name: _ type _ name; enum _ name: _ type
  4. # Endif # else
  5. # Define NS_ENUM (_ type, _ name) _ type _ name; enum # define NS_OPTIONS (_ type, _ name) _ type _ name; enum
  6. # Endif

     

    Typedef NS_ENUM (NSInteger, UIViewAnimationTransition ){

    Expand:

    Typedef enum UIViewAnimationTransition: NSInteger UIViewAnimationTransition;
    Enum UIViewAnimationTransition: NSInteger {
     

    From the enumeration definition, NS_ENUM and NS_OPTIONS are essentially the same, and they are used only literally. NS_ENUM is a general condition. NS_OPTIONS is generally used to define a condition (bitmask) with a displacement operation or characteristics ).

    In actual use, you can directly define:

    Typedef enum: NSInteger & nbsp; {...} UIViewAnimationTransition;

    It is equivalent to the preceding definition.

     

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.