# Ifdef, # ifndef, # else, # endif execution condition compilation,
The program we developed should not only run on pc, but also on mobile. At this time, the program will execute selective compilation based on the machine environment. For example, compile the PC-side program and the mobile-side program on the Mobile End. Here we can compile the program with two sets of conditions. Let's take a look at # ifdef... # endif; Syntax format: # ifdef macro name | # ifdef macro name // any code | // any code # endif | # else | // any code | # The two formats above endif are, if a macro is specified, # ifdef... # code in endif.
//// Main. m // Hong_Test /// Created by Cheng Yingying on. // Copyright, 2017, Cheng yingmiao. all rights reserved. // # import <Foundation/Foundation. h> # define iPadint main (int argc, const char * argv []) {@ autoreleasepool {# ifdef iPad NSLog (@ "this is ipad "); # else NSLog (@ "this is iphone"); # endif // insert code here... NSLog (@ "Hello, World! ");} Return 0 ;}The execution result is 22:54:22. 010569 Hong_Test [8061: 318230] this is ipad. Let's analyze it. First, we define macro # define iPad. Next, the program will search for the macro, the program to be compiled will be compiled according to our specific conditions. In fact, it is similar to if... else... if. But why is there such a thing? There are two things: 1. we will write a lot of statements when debugging the program. In the official deployment, we only need to remove the Macro. Second, such Conditional compilation can reduce the size of the program. Next we will use # if... # else... # endif for an instance drill to end today's learning.
//// Main. m // Hong_Test /// Created by Cheng Yingying on. // Copyright, 2017, Cheng yingmiao. all rights reserved. // # import <Foundation/Foundation. h> # define age 30int main (int argc, const char * argv []) {@ autoreleasepool {# if age> 60 NSLog (@ ""); # elif age> 40 NSLog (@ "middle-aged"); # elif age> 20 NSLog (@ ""); # endif} return 0 ;}
Result: young people