Using NSLog to Debug Your iPhone Application

Posted by Jan Vantomme on 24 January 2009.
Tags: , ,

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 %@."myNumbermyText); 

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

Oldskool Comments (2)

Gravatar for Freek

From: Freek
Date: 30.12.2010

In NSLog you should also use the @. like this: NSLog(@"number: %i", number); -

Top · Permanent link to this comment

Gravatar for Jan Vantomme

From: Jan Vantomme
Date: 30.12.2010

Thanks for letting me know. I'm using Textile markup language for my articles and sometimes things can go wrong with code examples. Fixed it.

Top · Permanent link to this comment