Given #define LOG_OBJECT(object) (NSLog(@"" #object @" %@ %@:%d”, [object description], [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__));
The statement LOG_OBJECT(self);
will produce:
2014-07-18 17:43:30.041 FrogCamera[12484:2144843] self ViewController.m:20
I wish to understand how the preprocessor code works. How can I see the statement that the pre processor produced?
Specifically:
( )
?#object
a text replacement for what ever value was provided ?@""
before the #object
? @“” #object @“ %@ %@:%d”
is transformed to @"self %@ %@:%d"
?Here’s a usage example:
@interface ViewController ()
#define LOG_OBJECT(object) (NSLog(@"" #object @" %@ %@:%d", [object description], [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__));
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
LOG_OBJECT(self);
// = 2014-07-18 17:48:19.439 FrogCamera[12597:2151087] self <ViewController: 0x79755000> ViewController.m:20
(NSLog(@"self %@ %@:%d", [self description], [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__));
// = 2014-07-18 17:48:19.440 FrogCamera[12597:2151087] self <ViewController: 0x79755000> ViewController.m:21
}
@end
See GlobalUtilities.h for origin of LOG_OBJECT statement.
This is rather simple:
#define SUM(A, B) A + B
SUM(1, 1) * 3 //the expected result is 6, the real result is 1 + 1 * 3 = 4
Adding parenthesis around every macro is a good programming convention to prevent this type of errors.#param
is a stringification operator. It wraps the parameter in quotes - e.g., parameter value
is converted to "value"
.char *
). The author wanted a NSString *
, that's why he is adding an @
in the beginning. Note that @"aaa" "bbb"
is equivalent to @"aaabbb"
(this feature enables us to split strings over multiple lines). However, this is not very elegant. Using a %s
would make it a bit simpler.@"" "self" @" %@ %@:%d"
. The compiler considers consecutive strings as one string.