IOS preprocessing statement and ios preprocessing statement
The source code computer in the program cannot be identified. You need to convert the written code into 0 or 1 binary code before the computer can recognize it. It takes two steps to convert source code into binary code: Compilation and link. Compiling is to convert the code of each file into binary code through the compiler. In this process, if there is a syntax error, a prompt indicating that the compilation fails. If the compilation is successful, multiple Target files are generated. Other files may be used in one file. Therefore, you need to combine the target file generated by compilation with the file provided by the system. This process is a link. Finally, an executable file is generated.
Generally, people understand that the program runs in two phases: Compilation and linking. But in fact, the pre-processor needs to perform preprocessing before compilation, and then enters the compilation phase after processing. Since the pre-processing command is executed before compilation, it is more efficient than running the program.
The Preprocessing Program is actually a statement that is processed before the analysis program. It can identify specific statements in the program. All preprocessing statements start with a pound sign (#), which must be the first non-empty character in a line.
Pre-processing statements can be divided into three types: File Inclusion, macro definition, and Conditional compilation. Here we will explain them one by one.
File Inclusion
File Inclusion means that when the current file uses functions or methods in other files or other information, you can include the header files of other files and then use them in the current file, file Inclusion is generally placed at the beginning of the file.
If you use C language programming, the file inclusion is # include <> or # include "". If you use Objective-C, the file contains # import <> or # import "". # The biggest difference between # include and # import is that # import performs a deduplication check during File import. In addition, there are two differences between "" and <>. "" is used to store self-written files, and <> is used to store system files. During execution, the program will first find the corresponding types of files based on the style you wrote. For example, <>, the system file will be searched first. If the system file cannot be found, the custom file will be searched again. Therefore, choosing the correct style can improve the program running efficiency.
When file inclusion is used, file A is used in file B, and file B is used in file A. The mutual use of the Inclusion relationship is A bit like an endless loop, to use File A, you must first have file B, but in file B, you must first have file A. Therefore, an error occurs during running. The best way to solve this problem is to replace file inclusion with @ class, which indicates that this class exists, and include the file when it is actually used in the source file.
Code:
//. H // Test // Created by jerei on 15-7-24. // Copyright (c) 2015 jerehedu. all rights reserved. // # import <Foundation/Foundation. h> @ class B; @ interface A: NSObject @ property (nonatomic, strong) B * obj; @ end // B. h // Test // Created by jerei on 15-7-24. // Copyright (c) 2015 jerehedu. all rights reserved. // # import <Foundation/Foundation. h> @ class A; @ interface B: NSObject @ property (nonatomic, strong) A * obj; @ end
Macro definition
In a program, some constants or short functions are repeatedly used. For these commonly used data, we can use macro definitions. Using macro definition can quickly complete the configuration in multiple places in the program. The biggest advantage is that as long as the macro definition value is modified, all macro-defined values will change. In addition, macro definitions are replaced and set before the program is compiled, which is more efficient than defining global variables or functions.
Macro definition is implemented through # define, which is generally written under the file of the program. The macro name is usually expressed in uppercase letters. Next, let's take a look at the use of macro definition through code.
Code:
// Main. m // Test // Created by jerei on 15-7-24. // Copyright (c) 2015 jerehedu. all rights reserved. // # import <Foundation/Foundation. h> # define JR_PI 3.14 # define JR_MAX (a, B) (a> B )? (A) :( B) // obtain the two numbers that are greater than limit # define JR_SQUARE_1 (n) n * n // obtain the square of the number # define JR_SQUARE_2 (n) * (n) // calculates the square of the number # define JR_HELLO @ "hello world"; int main (int argc, const char * argv []) {@ autoreleasepool {int num1 = JR_MAX (1, 2); NSLog (@ "max = % I", num1); // result: max = 2 int num2 = JR_SQUARE_1 (2); NSLog (@ "2 square = % I", num2); // result: 2 square = 4 int num3 = JR_SQUARE_1 (2 + 1); NSLog (@ "(2 + 1) square = % I", num3 ); // result: (2 + 1) square = 5 int num4 = JR_SQUARE_2 (2 + 1); square of NSLog (@ "(2 + 1) = % I ", num4); // result: (2 + 1) square = 9} return 0 ;}
As you can see in the code, we can also find the square of a number, but the results of the two macros are different. The first calculation of the square of 2 + 1 is 2 + 1*2 + 1, so the result is 5, and the answer is incorrect. Therefore, when writing macro definitions with parameters, you need to set parentheses to ensure correctness.
Conditional compilation
In fact, the pre-processor determines the condition based on the pre-processing statement before compiling. If the condition is met, the code segment below the condition is compiled. If the condition is not met, the following code segment does not enter the compilation stage.
Conditional compilation is mainly divided into two types. One is to determine whether a macro has been defined, and decide whether to compile a specific piece of code based on whether the macro has been defined. In addition, there are a set of statements that are very similar to the tier-if structure in the condition structure, but there are some differences in writing: # if, # elif, # else, # endif. Note that the # endif end flag is required in either case. In addition, the most important thing is that normal variables cannot be used in conditions during Conditional compilation, and macro definitions are generally used.
Code:
// Main. m // Test // Created by jerei on 15-7-24. // Copyright (c) 2015 jerehedu. all rights reserved. // # import <Foundation/Foundation. h> # define JR_COUNT 10int main (int argc, const char * argv []) {@ autoreleasepool {# if defined (JR_COUNT) NSLog (@ "defines the macro COUNT "); # endif # if defined (JR_MAX) NSLog (@ "the macro JR_MAX is not defined"); # endif # if JR_COUNT = 1 NSLog (@ "JR_COUNT = 1 "); # elif JR_COUNT = 2 NSLog (@ "JR_COUNT = 2"); # elif JR_COUNT = 3 NSLog (@ "JR_COUNT = 3 "); # else NSLog (@ "JR_COUNT = % I", JR_COUNT); # endif} return 0 ;}
For questions or technical exchanges, please join the official QQ group: (452379712)
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.