Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
April 22, 2018, 09:21:03 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Convenient and Elegant method of Debugging  (Read 1917 times)
Posts: 10

« on: March 25, 2009, 10:41:16 PM »

While working on the fraction calculator, I needed to do some debugging, and I tried writing my debug statements like this:

Code: (Objective-C)
-(void) storeFracPart
if (firstOperand){
#ifdef DEBUG
NSLog(@"yes firstOperand");
if (isNumerator){
myCalculator.operand1.numerator = currentNumber ;

I then created debugging.h, set it to be imported, and switched between "debug mode" and not "debug mode" (note here, I am not referring to XCode's debugger) by swapping between these two lines  in debugging.h:
Code: (Objective-C)
#define DEBUG
#undef DEBUG

This is all based on the book, ch. 12

I was wondering if anyone had a more elegant method. Specifically, I'd like to be able to add my debugging lines without adding 2 lines of code everytime. Something like
Code: (Objective-C)
 ? DEBUG : NSLog(@"yes firstOperand");

would be so much more readable, quicker, and space-saving.
Any ideas?
Hero Member
Posts: 3114

« Reply #1 on: March 26, 2009, 02:07:19 AM »

If you're just using NSLog, for example, for your debugging, then a quick trick would be to insert the following at the start of your program:

Code: (Objective-C)
#define NSLog

when you don't want any of the NSLog output.  This replaces NSLog with nothing in your program, and you end up with a bunch of comma-separated expressions after the preprocesor does its thing.  Note that those expressions will still be evaluated, so there can be some run-time overhead if they're complex.

If you have some NSLog calls whose output you want and some that are there just for debugging, you can change the latter to another name like Debug and then put this at the start of your program:

Code: (Objective-C)
#define Debug NSLog

when you want to debug


Code: (Objective-C)
#define Debug

when you don't.

Those are two quick ideas.


Steve Kochan
« Last Edit: March 26, 2009, 05:18:10 AM by skochan » Logged
Posts: 10

« Reply #2 on: March 26, 2009, 08:05:39 AM »

Actually more simple than I expected, not even an if clause!

Thanks a bunch.
Pages: [1]   Go Up
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 All rights reserved.