Using NSLog to Debug Your iPhone Application
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
Related Articles
- A Sneak Peek At My First iPhone App (Blog)
- Ad Hoc Distribution of iPhone Apps (Blog)
- Building a Custom WordPress for iPhone App (Blog)
- OpenGL ES for iPhone: Drawing a Circle - Part I (Blog)
- OpenGL ES for iPhone: Drawing a Circle - Part II (Blog)
- OpenGL ES for iPhone: Drawing a Circle - Part III (Blog)
- ReBirth for iPhone (Things to Read, External Link)
- Random Numbers in Objective-C (Blog)
- How to create a FullScreen iPhone Application (Blog)
Browse Articles
Comments (2)
From:Freek
Date:30.12.2010
In NSLog you should also use the @. like this:
NSLog(@“number: %i”, number); –
Popular Articles
- Introduction to openFrameworks
- Creating 3D Shapes with Hemesh
- Mirroring Video with openFrameworks
- An Introduction to colorLib
- How to create a FullScreen iPhone Application
Popular Tags
- processing (90)
- software (50)
- web design (40)
- art (39)
- css3 (37)
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