Writing to the console log is something that you’ll end up doing sooner rather than later. Usually you’ll write to the log when your testing prototype code. Other times, you may be simply debugging your code (although there are better ways to debug).
NSLog
Objective and primitive type values may be reported to the console using NSLog. Each type has a different specifier that must be used as a placeholder for the value. You will type out the string that you would like to appear in the console while putting in specifers into the string where you would like to see values reported. You can put as many specifers into the string as you like, but you must make sure to include each value in the call to NSLog.
For example, if I had an integer variable named myInteger and a character variable named myCharacter and I wanted to report each of these values to the console I would do something like this:
NSLog(@”myCharacter = %c and myInteger = %i”, myCharacter, myInteger);
Warning Each specifier that you include in the NSLog string must have a corresponding value in the comma-seperated list to the right or the compiler will throw at error more ‘%’ conversions than data arguments at compile time.
How to Use NSLog
NOTE: you will need a Mac or iOS application set to try out these examples of NSLog . Click here to follow instructions on setting up an iOS app using Xcode.
Open up your applications main.m file. Your code will look something like this:
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char *argv[]){ @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
Stick the code that we want to test on the next line right after the @autorelease statement.
Examples of NSLog
Add and examine these NSLog statements to your application to see different ways that NSLog is used.
//To print out primitive types: int myInteger = 1; NSLog(@"myInteger = %i", myInteger); float myFloatingPointNumber = 2; NSLog(@"myFloatingPointNumber = %f", myFloatingPointNumber); NSLog(@"myFloatingPointNumber in scientific notation = %e", myFloatingPointNumber); NSLog(@"myFloatingPointNumber = %f", myFloatingPointNumber); NSLog(@"myFloatingPointNumber in scientific notation = %e", myFloatingPointNumber); char myCharacter = 'A'; NSLog(@"myCharacter = %c", myCharacter); //To print out the % symbol NSLog(@"Percent Sign looks like %%"); //To print out Objective-C objects: NSString *myString = @"My String"; NSLog(@"myString = %@", myString); NSLog(@"myString's pointer = %p", myString); //To print out a series of values NSLog(@"myCharacter = %c and myInteger = %i", myCharacter, myInteger);
To test this code out, hit the Run button located in the upper lefthand area of Xcode. You should see output like this:
myInteger = 1 myFloatingPointNumber = 2.000000 myFloatingPointNumber in scientific notation = 2.000000e+00 myFloatingPointNumber = 2.000000 myFloatingPointNumber in scientific notation = 2.000000e+00 myCharacter = A Percent Sign looks like % myString = My String myString's pointer = 0x35d4 myCharacter = A and myInteger = 1
How to View the Console Log
You may not be able to locate your console window at first. With Xcode you can show and hide many panes that show content, code and utility widgets. Your console window should be located at the middle bottom area of Xcode. If you can see this window make sure that the View button strip located in the upper right hand area of Xcode has the middle button depressed.
Also make sure that the bottom window’s right pane button is depressed. Check out the screenshot below for the locations of these controls in Xcode:
Table of NSLog Placeholders
You can use the same placeholder specifiers with NSLog that you would use with the corresponding C functions to write to the log. Here is a list of the most important:
Specifier | Data Type |
---|---|
%@ | Objective-C object (looks at description method) |
%d, %D, %i | int (signed 32-bit integer) |
%u, %U | unsigned int (unsigned 32-bit integer) |
%f | double (64-bit floating point number) |
%e | double (64-bit floating point number in scientific notation) |
%c | unsigned char (unsigned 8-bit character) |
%C | unichar (16-bit character) |
%p | Pointer printed in hexadecimal |
%% | Escape character so we can print the % sign |