IOS-----5 minutes to learn the correct use posture of enumeration-enumeration macros

Source: Internet
Author: User

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)?
    1. Apple recommends.
    2. That's what everybody writes.
    3. Others are not good at learning.
    4. 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:

    1. 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

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.