1, first come to a few commonly used:
is the HD screen
#define Isretina ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 960), [[UIScreen Mainscreen] currentmode].size): NO)
//whether simulator
#define Issimulator (Nsnotfound!= [[Uidevice Currentdevice] model] rangeofstring:@ "Simulator"].location)
//whether the
ipad #define ISPAD (ui_user_interface_idiom () = Uiuserinterfaceidiompad)
/whether the ipad
#define SOMETHING (ui_user_ Interface_idiom () = = Uiuserinterfaceidiompad)? Ipad:iphone
2, the basic use:
The definition pi value 3.1415926
#define PI 3.1415926
//Then can use double i=2*pi*3 as follows in the program use
;
The effect is equal to double i=2*3.1415926*3;
The preprocessing command can define any form that conforms to the format, such as determining whether the year is leap years
#define is_leap_year year%4==0&&year%100!=0| | Year%400==0
//use can be directly
if (is_leap_year)//
or you can define a parameter
#define is_leap_year (y) y%4= =0&&y%100!=0| | Y%400==0
//use can be directly
int ys=2012;
if (Is_leap_year (YS))
//Usually the preprocessor is defined on a line if a good branch, for example, is too long to be wrapped, you need to use the symbol "/" to indicate that there is another line, multirow, for example:
#Define Is_ Leap_year year%4==0&&year%100!=0/
| | Year%400==0
//macro definition parameter put a # then when you call the macro, the preprocessor creates a C-style constant string example based on the macro parameter:
#define STR (x) # x
//will make the subsequent call to the
NSLOG (STR (programming in objective-c./n));
Display results for programming in objective-c./n
3,about # and # # operators:
<1> the string operator # in a macro definition:
The function of # is to stringify the macro parameters that follow it, the meaning is to add a double quotation mark around it by replacing the macro variable it applies to. For example
#define WARN_IF (expr) \ Do
{\
IF (expr) \
fprintf (stderr, "Warning:" #EXPR "\ n"); \
} while (0)
The backslash in the code above is primarily used to translate line breaks, that is, to mask newline characters.
then the following code calls:
warn_if (divider = 0);
will be resolved as: do
{\
if (divider = 0) \
fprintf (stderr, "Warning:" "divider = 0" "\ n"); \
} while (0);
Note that the ability to manipulate strings must be macro parameters, not just some random substring (token).
<2>. Connector in macro definition # #:
The connector # #Used to connect two tokens into one token, but it may not precede the first token or after the last token. Note that the object is connected here as long as it is token, not necessarily a macro parameter, but # #It must be in a macro definition to be effective, because it is a compile-time concept (compare around).
#define Link_multiple (A, B, C, D) a# #_ # #b # #_ # #c # #_ # #d
typedef struct _RECORD_TYPE link_multiple (name, company, position, salary);
*
* The above code will be replaced with the
* typedef struct _RECORD_TYPE name_company_position_salary;
* *
also as the following example:
#define PARSER (N) printf ("token" #N "=%d\n", token# #N)
int token64 =;
Call macros as follows:
PARSER ();
will be parsed as:
printf ("token" "", "=%d\n", Token64);
In Obj-c, if I have the following definition:
#define _X (A, B) (a#b)
#define _XX (A, B) _x ([NSString stringwithformat:@ "%@_c", A], b)
GCC will make an error.
the correct wording is:
#define _XX (A, B) _x ([NSString stringwithformat:@ "%@_c", A]), b)
4, another macro definition object-c single example
#define Gtmobject_singleton_boilerplate (_object_name_, _shared_obj_name_) static _object_name_ *z# #_shared_obj_name
_ = nil; + (_object_name_ *) _shared_obj_name_ {@synchronized (self) {if z# #_shared_obj_nam E_ = = nil) {/* note "Self" may is the same as _object_name_ * * Firs T assignment done in Allocwithzone but we must reassign in case init fails/z# #_shared_obj_name_ = [[Self alloc] in
It];
_gtmdevassert (z# #_shared_obj_name_!= nil), @ "didn ' t catch singleton allocation"); } return z# #_shared_obj_name
_; } + (ID) Allocwithzone: (Nszone *) zone {@synchronized (self) {if (z# #_shared_obj_name_ = = nil) {z# #_shared_obj_name_ = [Super Allocwithzone:zone];
return z# #_shared_obj_name_; }/* We can ' t return the S
hared instance, because it ' s been init ' d/_gtmdevassert (NO, @ "Use the singleton API, not Alloc+init");
return nil;
}-(ID) retain {return self;
}-(Nsuinteger) Retaincount {
return Nsuintegermax;
}-(void) Release {}
-(ID) autorelease {return self; }-(ID) Copywithzone: (Nszone *) zone{return self; }
5, conditional compilation:
#if!defined (fcdebug) | | Fcdebug = 0
#define FCLOG (...) do {0)
#define FCLOGINFO (...) do {} while (0)
#define FCLOGERROR (...) Do {} while (0)
#elif fcdebug = 1
#define FCLOG (...) NSLog (__va_args__)
#define FCLOGERROR (...) NSLog (__va_args__)
#define FCLOGINFO (...) do {} while (0)
#elif fcdebug > 1
#define FCLOG (...) NSLog (__va_args__)
#define FCLOGERROR (...) NSLog (__va_args__)
#define FCLOGINFO (...) NSLog (__va_args__)
#endif
6, reference to the C language preprocessing command introduction:
#define Define a preprocessing macro
#undef to cancel the definition of a macro
#include include file commands
#include_next is similar to #include, but it has a special purpose.
#if the Conditional command in the compilation preprocessing, equivalent to the IF statement in C syntax
#ifdef Determine whether a macro is defined, and if so, execute the following statement
#ifndef, contrary to #ifdef, determines whether a macro has not been defined
#elif if the #if, #ifdef, #ifndef or the previous #elif conditions are not satisfied, then execute the statement after #elif, equivalent to the else-if in C syntax
#else and #if, #ifdef, #ifndef correspondence, if these conditions are not satisfied, then execute the statement after #else, equivalent to the else in C syntax
#endif #if, #ifdef, #ifndef End flags for these conditional commands.
Defined and #if, #elif together, to determine whether a macro is defined
#line flag the line number where the statement is located
# Replace macro arguments with character-channeling constants with parameter values as content
# # Connect two adjacent tags (token) to a single tag
#pragma description Compiler information #warning display compilation warning information
#error Display compilation error messages
Reference link: http://www.uml.org.cn/c++/200902104.asp