Objective
Enums, enumerations, believe that most programming languages have corresponding enumeration types, and may have fewer features, but the most central feature of enumerations is " State, status code, Options " in the canonical definition code.
Status, status code, options
What is a state: there can only be one value at a time (the status code is his value), such as the enumeration in this scrollview:
objective-c
12345 |
typedef ns_enum(nsinteger, uiscrollviewkeyboarddismissmode) { uiscrollviewkeyboarddismissmodenone, Uiscrollviewkeyboarddismissmodeondrag, uiscrollviewkeyboarddismissmodeinteractive }; |
What is an option: one or more values can appear at the same time:
objective-c
123456789 |
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 /c5> }; |
Why use Ns_enum (state) and ns_options (options)?
- Apple recommends.
- That's what everybody writes.
- Others are not good at learning.
- None of the above is important, just look at my proof .
State of presentation posture wrong posture
objective-c
123456 |
#define Uiscrollviewkeyboarddismissmodenone 0 #define UISCROLLVIEWKEYBOARDDISMISSMODEONDRAG 1 #define Uiscrollviewkeyboarddismissmodeinteractive 2 //Receive values like this: int uiscrollviewkeyboarddismissmode = uiscrollviewkeyboarddismissmodenone; |
Why not do it well:
- A macro definition has no type constraints, just a simple substitution.
- Poor readability.
- You can use int or Uiinteger to receive it.
Correct posture
objective-c
12345678 |
typedef enum { uiscrollviewkeyboarddismissmodenone = 0, Uiscrollviewkeyboarddismissmodeondrag, uiscrollviewkeyboarddismissmodeinteractive } uiscrollviewkeyboarddismissmode; //Indicates enumeration typeuiscrollviewkeyboarddismissmode mode = uiscrollviewkeyboarddismissmodenone; |
That's how it's used:
objective-c
12345678910111213 |
- (void)dealwithmode:(uiscrollviewkeyboarddismissmode)mode { Switch (mode) { Case uiscrollviewkeyboarddismissmodenone: //...Break ; Case uiscrollviewkeyboarddismissmodeondrag: //...Break ; Case uiscrollviewkeyboarddismissmodeinteractive: //...Break ; }} |
This enumeration solves the disadvantages of 1 and 2 above, but the 3rd is still unresolved, and I can still do this:
objective-c
123 |
int mode = uiscrollviewkeyboarddismissmodenone; //or Uiinteger mode = uiscrollviewkeyboarddismissmodenone; |
Perfect Posture
Ns_enum is an "upgrade" enumeration in Objective-c, with the following improved code:
objective-c
12345 |
typedef ns_enum(nsinteger, uiscrollviewkeyboarddismissmode) { uiscrollviewkeyboarddismissmodenone, Uiscrollviewkeyboarddismissmodeondrag, uiscrollviewkeyboarddismissmodeinteractive }; |
The code is simple and can be commented and answered if you don't understand it.
Use Posture for options
What if there is an "option variable" to represent one or more values at the same time swollen? Then we have to ns_options.
For example, there is a simple example:
objective-c
12345 |
typedef ns_options(nsuinteger, jzzpersontype) { jzzpersontypehigh = 0, jzzpersontyperich = 1 << 0, jzzpersontypepretty = 1 << 1 }; |
As you can see, our options here are defined in the way of bitwise operations , and the benefit is that our option variables can be represented as:
objective-c
12345678910111213 |
//with "or |" Operations assign multiple options simultaneouslyjzzpersontype persontype = jzzpersontypehigh | Jzzpersontyperich | jzzpersontypepretty; //Use the "and &" operation to remove the corresponding bitif (persontype & jzzpersontypehigh) { NSLog(@ "High"); }if (persontype & jzzpersontyperich) { NSLog(@ "Rich"); }if (persontype & jzzpersontypepretty) { NSLog(@ "Handsome"); } |
| After the assignment, the rich Heart (memory) is actually long like this:
IOS-----5 minutes to learn the correct use posture of enumeration-enumeration macros