Objective-C Language Logging Logging Variable Values

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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.

Instead, always make the first argument a format specifier, like this:

NSLog(@"%@", anObjectVariable);
NSLog(@"%d", anIntegerVariable);


Got any Objective-C Language Question?