Wednesday, April 7, 2010

iPhone NSLog Basics

I'm most comfortable in Java. Ruby is favorite of mine as well. Lately, I've been diving into the iPhone platform. Objective-C is a beast compared to the Simula-based languages. My plan is to have lots of short notes on the frustrating little things that I'm learning.



CocoaDev has an awesome overview of printing to NSLog. This is absolutely the quickest and easiest way to debug on the iPhone. I was having a terrible time printing an integer to the log, being the Objective-C newbie that I am. NSLog takes printf syntax.



I was trying to parse the number of questions stored in a string and print that number to the log. I wrote a method in the same class that counted the number of questions in a string and returned an integer. Pitfalls:


  1. I forgot to send the method to the self object

  2. I didn't know the syntax for printf or NSLog


The correct solution looked just like this:

NSLog(@"you have %d questions", [self parseNumberOfQuestions]);

No comments:

Post a Comment