Tutorial by Examples

NSLog(@"Log Message!"); NSLog(@"NSString value: %@", stringValue); NSLog(@"Integer value: %d", intValue); 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 forma...
NSLog(@"NSLog message"); printf("printf message\n"); Output: 2016-07-16 08:58:04.681 test[46259:1244773] NSLog message printf message NSLog outputs the date, time, process name, process ID, and thread ID in addition to the log message. printf just outputs the message. N...
NSLog(@"NSLog message"); The message that gets printed by calling NSLog has the following format when viewed in Console.app: DateTimeProgram nameProcess IDThread IDMessage2016-07-1608:58:04.681test[46259:1244773]NSLog message
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 pars...
When NSLog is asked to print empty string, it omits the log completely. NSString *name = @""; NSLog(@"%@", name); // Resolves to @"" The above code will print nothing. It is a good practice to prefix logs with labels: NSString *name = @""; NSLog(@&quo...
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. #ifdef DEBUG #define DLog(...) NSLog(__VA_ARGS__) #els...
NSLog(@"%s %@",__FUNCTION__, @"etc etc"); Inserts the class and method name into the output: 2016-07-22 12:51:30.099 loggingExample[18132:2971471] -[ViewController viewDidLoad] etc etc
There is no format specifier to print boolean type using NSLog. One way to print boolean value is to convert it to a string. BOOL boolValue = YES; NSLog(@"Bool value %@", boolValue ? @"YES" : @"NO"); Output: 2016-07-30 22:53:18.269 Test[4445:64129] Bool value YES ...
NSLog(@"%s %d %s, yourVariable: %@", __FILE__, __LINE__, __PRETTY_FUNCTION__, yourVariable); 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 ca...
NSLog is good, but you can also log by appending to a file instead, using code like: NSFileHandle* fh = [NSFileHandle fileHandleForWritingAtPath:path]; if ( !fh ) { [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil]; fh = [NSFileHandle fileHandleForWriting...

Page 1 of 1