Using NSLog to Debug Your iPhone Application

Posted on 2009-01-24 by Jan Vantomme
Tags: tutorial

I've done a lot of generative design in the last years. First in Macromedia Director and for the last two years I've been using Processing. Somewhere last week I started to play with the iPhone SDK and it's quite easy to create small applications. I'll probably create a small generative iPhone application in 2009 and write about what I'm learning right here.

Debugging Processing

Logging messages to the console in Processing is really easy. Just use the println(); function like in the example below.

int myNumber = 3;
String myText = "Dog";
println("My number is " + myNumber + " and my text is " + myText + ".");

This little snippet of code will log the text "My number is 3 and my text is dog." to the console.

Debugging Objective-C

If you would convert this snippet code to Objective-C, you will get something like this:

int myNumber = 3;
NSString *myText = @"Dog";
NSLog(@"My number is %i and my text is %@.", myNumber, myText);

The NSLog function replaces %i with myNumber and %@ with myText. %i and %@ are called format specifiers. They tell the NSLog function what type of variable myNumber and myText are. %i is used to represent an integer, %@ is used to represent an object. In this case myText is an NSString object.

Format Specifiers

And to finish this article i'll give you a short list with the most common format specifiers you'll need to debug your application.

  • %i, %d signed integer
  • %u unsigned integer
  • %f float
  • %@ object

Tweet this article