iOS macro definitions, constants, used with enumerations

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/WangErice/article/details/51537888

Development process often requires the use of some global constants, and facilitates parameter passing between the method of determining the type and the like, are often used to define macros, const enumeration constants, and the like, we often use macro definitions to define the global constants but is not all global constants macros are the best choice?

Macro definitions are global constants defined method we use most often, very easy to use, in the course of the following considerations:

1. The macro definitions precompiled process has been completed before the program start compiling;

2. Character substitution macro definition only, and no priority restrictions, such as

#define add(x,y)x + y

If during use the add (3,4) * add (3,4), the result after macro definition is replaced 4 + 3 * 4 + 3 = 19, rather than (3 + 4) * (3 + 4) = 49, we must pay attention! ! !

3. The macro definition does the type of syntax checking, all the parameters in the macro definition are not the type of macro, may be problematic duplicate definition, if repeated define the same macro name, the program will only be given a warning, and not It will affect compiled, so if you have defined two macros with the same name, and the replacement is not the same, it is difficult to find.

4. The macro definition not variable definition, it does not allocate memory;

The macro definition only replacement string, the end use does not require the use of a semicolon.


const variable is modified symbol in the definition of global variables, it should give priority to add static const to replace macro constants, for the following reasons:

1. When the project is relatively large, the compiler will make the project too much macro definition becomes slow;

2.const modifying variables during compilation will check syntax, you can prevent accidental type of error, and repeat the definition, you can make the definition more secure;

When 3.const modified variable, which is not allowed to change, to prevent accidental modification constant;

4. Where you want to use, only need to use the extern keyword to expand the scope of the variable that variable access more flexible.

For use const, we need to note the following:

1.const modified common variable, the variable is expressed as a constant, not modify the course;

2.const modified class pointer variable; the variable containing the address to be divided into right and left parts *, const modified when the left portion contains symbols, not the address of the content alterations; const right side portion further modification, not the address of the variable changing, if both sides have const modification, the memory address and the contents are not variable;

3.const parameter can be modified, the modified parameters within the method can not be modified;

4. Scope was modified const variables can be extended by extern do not need to initialize and allocate space, just tell the compiler from the variable is defined elsewhere.


For a variety of integer variables are mutually exclusive (can not have two states) represented generally recommended to use NS_ENUM said the use of very simple, very convenient. Such as text alignment state in the vertical direction

typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {

    UIControlContentVerticalAlignmentCenter  = 0,

    UIControlContentVerticalAlignmentTop = 1 ,

    UIControlContentVerticalAlignmentBottom = 2 ,

    UIControlContentVerticalAlignmentFill    = 3,

};

The advantage of this is represented by the general integer may be more practical digital indistinguishable state, represented by a string of easily distinguishable, see the name EENOW, less error-prone, easily expressed. However, this method represents a deficiency is not co-exist between the various states, but there are times when we need to use multiple states exist, such as selecting the type of information in the address book of a need to get in contact

typedef NS_OPTIONS(NSUInteger, APContactField)

{

    APContactFieldFirstName = 1 << 0 ,

    APContactFieldLastName = 1 << 1 ,

    APContactFieldCompany = 1 << 2 ,

    APContactFieldPhones = 1 << 3 ,

    APContactFieldEmails = 1 << 4 ,

    APContactFieldPhoto = 1 << 5 ,

    APContactFieldThumbnail = 1 << 6 ,

    APContactFieldPhonesWithLabels = 1 << 7,

    APContactFieldCompositeName = 1 << 8 ,

    APContactFieldAddresses = 1 << 9 ,

    APContactFieldRecordID = 1 << 10 ,

    APContactFieldCreationDate = 1 << 11 ,

    APContactFieldModificationDate = 1 << 12 ,

    APContactFieldMiddleName = 1 << 13 ,

    APContactFieldSocialProfiles = 1 << 14 ,

    APContactFieldNote = 1 << 15 ,

    APContactFieldLinkedRecordIDs = 1 << 16 ,

    APContactFieldJobTitle = 1 << 17 ,

    APContactFieldWebsites = 1 << 18 ,

    APContactFieldBirthday = 1 << 19 ,

    APContactFieldSource = 1 << 20 ,

    APContactFieldRelatedPersons = 1 << 21 ,

    APContactFieldEmailsWithLabels = 1 << 22 ,

    APContactFieldDefault          = APContactFieldFirstName | APContactFieldLastName |

                                     APContactFieldPhones | APContactFieldRecordID,

    APContactFieldAll              = 0xFFFFFFFF

};

If you need to select the name and contact details, only need to define = _enums APContactFieldFirstName | APContactFieldLastName | APContactFieldPhones on it,

So the question is, for this you can select multiple attributes at the same enumerated types, how to determine an enumeration value (such as APContactFieldFirstName ) is not included in the selected enumeration (such as _enums) in it?

Only need to ( APContactFieldFirstName ) & (_ enums) operation, if it is true, contains false otherwise.


Guess you like

Origin blog.csdn.net/WangErice/article/details/51537888