[Proficient OBJECTIVE-C] pre-processor

Source: Internet
Author: User


[Proficient OBJECTIVE-C] pre-processor


Reference book: "Proficient objective-c" "Beauty" Keith Lee


Directory




    • Proficient in objective-c pre-processor
      • Directory
      • Preprocessor overview
      • Preprocessor language
        • Preprocessor directives
        • Macro




Preprocessor overview


Preprocessing replaces the input sequence of characters with some sequence of characters, based on a series of predefined rules. These operations are mainly divided into the following three steps:


Created with Rapha?l 2.1.0input source fileperform text translationsplits the input source file into multiple tokensconverting the input code to the preprocessor language


1. Text translation: Preprocessing splits the input source files into lines of code, replaces three-letter combinations with a single character, merges broken contiguous lines into longer lines of code, and replaces comments with a single space. A three-letter combination is a three-character sequence used to represent a single character in the C language.
2. Token conversion: The preprocessor converts the code processed in the previous step to a sequence of tokens.
3. Conversion based on preprocessor language: If a sequence of tokens contains preprocessing language elements, they are converted according to these tokens.



The first two operations are performed automatically, and the last action is performed by the preprocessor language function added to the source file.


Preprocessor language


The preprocessor language is a completely separate programming language. The translation of the source files by the preprocessing language mainly includes the contents of the source files, conditional compilation and macro expansion. The preprocessor language element processes the source file before the program compiles, but the preprocessor does not recognize the OBJECTIVE-C code.


Preprocessor directives


preprocessor directive format:


#Instruction name instruction parameter


The preprocessor directive starts with #, followed by the instruction name, followed by the corresponding parameter:


#import "Atom.h"


The preprocessor directive uses the line break symbol as the closing symbol. To extend a preprocessor directive to multiple lines, use a backslash \ To concatenate two lines of code:


 
 
#define DegreesToRadians(x) \
        ((x)*3,14159/180)


Preprocessor directives mainly include the following 4 categories: header file inclusion, conditional compilation, diagnostics, #pragma指令



Header file contains class directives with # include and #import


 
#include <Foundation/Foundation.h>
#include "Atom.h"
#import <Foundation/Foundation.h>
#import "Atom.h"


The difference between double quotation marks and angle brackets is that when you use double quotation marks, the compiler first searches for the included header files from the directory where the source files are stored. If not found, the compiler searches the default directory for header files, which are pre-configured directories for searching system standard header files, and when using angle brackets, the compiler searches the default directory for included header files directly. As a rule, you should use angle brackets to encapsulate standard header files, while other files are enclosed in double quotation marks.



The difference between #import and # include is that it #import可确保头文件仅在源文件中被包含一次, and thus prevents recursion from being included. For example, in the previous chapters ([Proficient objective-c] objects and messaging), the source file main.m contains header files Hydrogen.h and Atom+nuclear.h, and all two files contain header file Atom.h, via # The import directive contains header files Hydrogen.h and atom+nuclear.h so that main.m contains only the header file Atom.h once, and if you use # include, you need to include the guard in the header file Atom.h:


 
#ifndef ATOM_H
#define ATOM_H
@interface Atom : NSObject
......
@end
#endif


Conditional compilation directives have # if, #elif, #else, #endif, #ifdef和 #ifndef can be determined to include or not contain some or all of the source text, depending on whether the condition is set.



Conditional compilation Directives # If, #elif和 #else usage is similar to the IF, else if, and else in objective-c, except to add #endif when the entire conditional statement is ended. Similarly, # if and #endif can also be nested.


// The preprocessor expands the INPUT_ARGS identifier. If the identifier is a macro command, it will be replaced with the corresponding value. If it is not or the macro has no value, it will be replaced with 0.
#if INPUT_ARGS <= 0
#warning "No input arguments defined"
#elif INPUT_ARGS> 100
#error "Input arguments are too many"
#else
#define Sum INPUT_ARGS
#endif 


The previous containment guards were used by #ifdef and #ifndef.


 
#ifndef ATOM_H
#define ATOM_H
@interface Atom : NSObject
......
@end
#endif


Equivalent to


 
#if !defined ATOM_H
#define ATOM_H
@interface Atom : NSObject
......
@end
#endif


The diagnostic class preprocessor directives have #warning, #error和 #line.



Here are the uses of #warning and #error:


#warning "No input arguments defined"#error "Input arguments are too many"


And #line's syntax is


#line line number "file name"


The line number will be given the next line of code, and subsequent lines of code will have a line number with each row plus 1, and when compiling an error, the compiler will display error confidence with the wrong file name and the corresponding line number. Because most compilation tools can display the line number of the source file, as well as error and warning information, it is seldom necessary to use #line.



Add #pragma directive to set the tab in the popup window


// Set the create dividing line in the popup window
#pragma mark-
// Set the label name in the popup window
#pragma mark Main
int main (int argc, const char * argv []) {
     @autoreleasepool {
     }
     return 0;
}
#pragma mark- 


As shown below, you can jump to a specified location by selecting a label at the label location, which is especially useful in large projects and classes:





Macro


A macro is a code snippet with a name. When a name is used in the source code, it is replaced by the code snippet it represents. You can define constant values only with preprocessor macro directives, and you can provide functions similar to those for input parameter values. Define a macro using # #, #undef Remove macro.



Macros can only perform simple substitutions, so be careful when using macros. Take one of the following squares to calculate the macro as an example


#define SQUARE(x) x * xint result = SQUARE(42);


The resulting result is not 36, but 14, because it isint result = SQUARE(4 + 2);equivalent toint result = 4 + 2 * 4 + 2;.
If you want to get the results we want, we should write:


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


In fact, you should use parentheses in the definition of a function macro and an object type macro, and you should use curly braces to encapsulate a multiline function macro that executes instead of the return value:


#define SWAP(a,b) {a^=b; b^=a; a^=b;}


Macros have powerful features, but code that relies heavily on complex macros can be difficult to maintain, so don't overdo it!



[Proficient OBJECTIVE-C] pre-processor


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.