The first argument of NSLog is an NSString containing the log message format. The rest of the parameters are used as values to substitute in place of the format specifiers.
The formatting works exactly the same as printf, except for the additional format specifier %@ for an arbitrary Objective-C object. This:
NSLog(@"%@", object);
is equivalent to:
NSLog(@"%s",[object description].UTF8String);
NSLog Output Format
NSLog(@"NSLog message");
The message that gets printed by calling NSLog has the following format when viewed in Console.app:
Date
Time
Program name
Process ID
Thread ID
Message
2016-07-16
08:58:04.681
test
[46259
:
1244773]
NSLog message
Logging Variable Values
You shouldn't call NSLog without a literal format string like this:
NSLog(variable);// Dangerous code!
If the variable is not an NSString, the program will crash, because NSLog expects an NSString.
If the variable is an NSString, it will work unless your string contains a %. NSLog will parse the % sequence as a format specifier and then read a garbage value off the stack, causing a crash or even executing arbitrary code(opens new window).
Instead, always make the first argument a format specifier, like this:
Messages printed from NSLog are displayed on Console.app even in the release build of your app, which doesn't make sense for printouts that are only useful for debugging. To fix this, you can use this macro for debug logging instead of NSLog.
Will log the file, line number and function data along with any variables you want to log. This can make the log lines much longer, particularly with verbose file and method names, however it can help to speed up error diagnostics.
You can also wrap this in a Macro (store this in a Singleton or where you'll need it most);