Some basic concepts of macro definition


Convert one parameter of a macro to a string

#define STR (ARG) #arg #define OC_STR (ARG) @#arg NSLog (@ "%s", STR (xxoo)); NSLog (@ "% @", OC_STR (DDCC)); output: xxoo DDCC


Stitching two macro parameters

#define, DEF_VAR (type, name), type, type##_##name, DEF_VAR (int, a) = 5; NSLog (@%d ', int_a); output: 5

C language prefab macros

__FILE__ file, full path __LINE__, current code line number, __DATE__ pre - compiled date, __TIME__ pre - compile time, __TIMESTAMP__ pre - compiled timestamp __FUNCTION__, current function name

“…” and “VA_ARGS”

“…” is the parameter parameter of a parameter macro.
“__VA_ARGS__” is a macro expansion of a variable argument

#define MY_LOG (STR,...) NSLog (@ "[" __FILE__ ":%d]" STR, __LINE__, __VA_ARGS__) MY_LOG ("%d%d%d", 1, 2, 3); / / macro expansion following NSLog (@ "[" "/Users/Name/Desktop/CDemo/CDemo/ViewController.m" "%d]" "%d D, 61,1,2,3%d%" output: 23); [/Users/Name/Desktop/CDemo/CDemo/ViewController.m:66]1 PS, there is little need to pay attention to macro precast C language in addition to __LINE__ after the start is with ""


If the __VA_ARGS__ is empty ## will swallow in front of the sign

#define EMPTY_LOG (STR,...) NSLog (@ "[" __FILE__ ":%d]" STR, __LINE__, ##__VA_ARGS__) MY_LOG ("SSS"); / / macro expansion following NSLog (@ "[" "/Users/Name/Desktop/CDemo/CDemo/ViewController.m" "%d]" "SSS", 61); / / here start at the end of many a "," is not in line with the grammatical structure of the OC / / if EMPTY_LOG. EMPTY_LOG ("SSS"); / / macro expansion following NSLog (@ "[" "/Users/Name/Desktop/CDemo/CDemo/ViewController.m" "%d]" "SSS", 61); output: [/Users/Name/Desktop/CDemo/CDemo/ViewController.m:61]sss