Objective-C: # define Usage Analysis

Source: Internet
Author: User
 

Original article address:

Http://blog.csdn.net/kindazrael/article/details/8108868

In C, Preprocessor is a powerful tool that makes your code easier to read and modify. With preprocessing code, you can redefine a part of the code to make your code more suitable for your style. Preprocessor is processed in advance before the code is compiled. The pre-processing code starts with a pound sign.

1. About #define

#defineDeclarations are mainly used to assign meaningful names to constants (or strings). For example, when you write a calendar program, you can define:

#define MONTHS_PER_YEAR 12

 

Note the format.#defineThe command is followed by the constant name, followed by the assigned replacement value, separated by spaces, and the end does not require a semicolon.

# Define constant name substitution value

In general, it is a habit to write all the names of the preprocessed constants in upper case, and separate words with underscores ). The reason for doing so is that the compiler processes this section#defineWhen the code is used, spaces are automatically searched, and spaces are considered to be replaced by values. This is also the reason why there is no extra points at the end of each line of code, because if a semicolon is added, the semicolon itself will be considered as a part of the value.

A defined constant name is not a variable. Once defined, its value cannot be changed.#defineThe Declaration is usually located at the top of a file, followed#importAfter the file is imported and defined. Note that,#defineThe declaration must be placed before the constant name is used (unlike the method that can be placed in any position in the file ).

Use#defineAnother example is to store some meaningful values, such

# Define PI 3.141592654 // calculate the area of the circle using "Pi * radius"

 

This is much more convenient than writing everywhere 3.141592654. (The change is also much faster)
 

2. More advanced use

Note that the function of preprocessing code is equivalent to a "search and replace" function in the entire code before compilation. This means#defineThere are many more complex and advanced uses.

Starting from 2πTWO_PIIt is defined as 2 π:

#define TWO_PI 2.0 * 3.141592645

 

Since we have previously defined π, we can also use the defined constant name in the definition to refer:

#define TWO_PI 2.0 * PI

 

In addition to constants, you can also use preprocessing to define any character or string in objective-C code, for example:

#define AND &&#define OR ||#define EQUALS ==if (y EQUALS 0 OR y EQUALS 1) // …

 

Rational Use#defineTo enhance the readability of the Code. Compare the following two lines of code:

If (Year % 4 = 0 & amp; Year % 100! = 0) | (Year % 400 = 0) // or # define is_leap_year (Year % 4 = 0 & year % 100! = 0) | (Year % 400 = 0) if (is_leap_year) // The code is more readable.

 

A definition should usually be completed in a line of code. If you need to manually empty rows, enter a backslash (\) after each line to connect several lines of code into one line. For example:

#define IS_LEAP_YEAR(y) (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)if ( IS_LEAP_YEAR(currentYear) ) 

 

 

3. Macro

#defineParameters can also be used in the declaration, and multiple parameters can be used; this feature is called "macro ":

For example:

#define SQUARE(x) ( (x) * (x) )

 

At this time,y = SQUARE(v + 1);Equivalenty = ( (v + 1) * (v + 1) );.

Two Parameters

For example, you can define:

#define CalcInterest(x,y) ( x * y )

 

Code:

int earnings = CalcInterest(10,5));

 

Multiple Parameters

For example, in the code, you need to frequently use[NSArray arrayWithObjects: object, ..., nil]This command. Exploitation#define, You can simplify the Code:

#define Array(FIRST, ...) [NSArray arrayWithObjects: FIRST, ##__VA_ARGS__, nil]

 

Where##__VA_ARGS__All content represented by ellipsis.
 

4. # operators and # Operators

# The operator can generate a string in the C-language format. For example:

# Define string (x) # x // then string (testing) = "testing"

 

# Operator used to connect two strings. Suppose you have a set of variables,x1Tox100. If you want to print the value of a variable. You can define it as follows:

#define printxvar(n) printf("%i\n", x ## n)

 

Therefore,printxvar(20);It will be recognizedprintf(“%i\n”, x20);. In this case, you only need to enterprintxvar(n);You can print outxn.

Actually, ## the most common usage is described above##__VA_ARGS__It can be used to capture all content represented by ellipsis.

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.