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:
- # 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
- # If (_ cplusplus) # define NS_OPTIONS (_ type, _ name) _ type _ name; enum: _ type
- # Else # define NS_OPTIONS (_ type, _ name) enum _ name: _ type _ name; enum _ name: _ type
- # Endif # else
- # Define NS_ENUM (_ type, _ name) _ type _ name; enum # define NS_OPTIONS (_ type, _ name) _ type _ name; enum
- # 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.