Preprocessing Program
The pre-processing process is part of the OC compilation process. It is used to identify specific statements distributed in the program and process these statements before analyzing the OC program. The Preprocessing Program Statement is marked with "#", and this symbol must be the first non-empty statement in a line.
# Define statement
It is also called macro definition. Its basic purpose is to specify a program constant for the symbol name. There are two formats: with and without parameters.
1. Without parameters:
The basic format is as follows:
# Define Identifier value
In oc, we often encounter the values TRUE and FALSE, which are actually the identifiers defined by the foundation framework:
# Define TRUE 1
# Define FALSE 0
Therefore, we can replace 1 and 0 with the TRUE and FALSE identifiers in subsequent programs;
If (gameOver = true)
{
......
}
# Define is often placed at the beginning of the program, after # import or # include statements. This is not necessary. It can appear anywhere in the program, but must appear before the statements that reference it.
The definition of a value does not include a simple constant value. It can also include expressions and any other content:
# Define TWO_PI 2.0*3.1415926
You can directly replace 2.0*3.1415926 in the following program.
2. With parameters:
You can add a bracket next to the identifier and define the parameter value in the brackets.
# Define IS_LEAP_YEAR (y) y % 4 = 0 & y % 100! = 0 | y % 400 = 0
Here IS_LEAP_YEAR (y) is defined to determine whether the year we entered is a leap year:
If (IS_LEAP_YEAR (2008 ))
......
Conditional compilation:
Conditional compilation is usually used to create programs that can be compiled and run on different computer systems. It is also often used to switch various statements in the program.
# Ifdef, # endif, # else, And # ifndef statements
# Ifdef IPAD
# Define kImageFile @ "barnHD.png"
# Else
# Define kImageFile @ "barn.png"
# Endif
If we have already defined an IPAD for the Preprocessing Program, we will define kImageFile as @ "barnHD.png"; otherwise, it will be @ "barn.png ". # Endif indicates the end judgment.
Another purpose is to execute a piece of code while debugging, but we do not want the code to run in actual operation. We can do this:
# Ifdef DEBUG
Code block to run
# Endif
# The ifndef statement is similar to the # ifdef statement, but the opposite is true. # ifndef is used to determine whether to run if the Preprocessing Program does not have a defined identifier. Otherwise, it does not run.
# If and # elif statements
# If Preprocessing Program statements provide a more common method for control condition compilation. It can be used to monitor whether the constant expression is non-zero. If it is non-zero, it will process the subsequent rows until # else, # elif, or # endif.
# Undef
# Undef is used to cancel macro definition. It can also control macro-defined scopes.
For example:
# Define IPAD
......
# Undef
At this point, the scope of the IPAD ends with # undef. If no # undef is available, the scope of the IPAD can be maintained until the end of the program.