views:

44

answers:

3

Hi, I was wondering if there is an effective way to debug problems in xcode while coding in Objective-C. I create webpages constantly and code in jquery and javascript where you can set various alert boxes in different places in your code to determine if your script is properly executing sections. Can you do something like that in xcode to make sure that your script is executing methods properly and creating variables properly?

Thanks

A: 

yes, the debugger can do all the things you want to do (just set some breakpoints - right click where you want them - then build&debug)

KevinDTimm
am I missing something? How does one, 'right click' on a mac?
sadmicrowave
With a mighty mouse, put your finger on the right side when you click. On a laptop, put two fingers on the trackpad (you might have to enable this in the Trackpad control panel). You can also hold down the control key before clicking, but that requires two hands. Mice and trackpads support other gestures; search for more if you're unfamiliar.
outis
ctrl-click should do the same thing
davbryn
@sadmicrowave - yes, you are missing something
KevinDTimm
A: 

You can try writing to the console.

NSLog(@"Some status message here");
NSLog(@"The value of myObject is:%@", myObject);

To view the output of your application, while running with Xcode, click Run->Console and you will see all of the output from your application.

NeilMonday
+2  A: 

Use the debugger - that's what it is there for! Set breakpoints by clicking in the grey are next to the line of code you want to break on. When this line of code is going to be excuted, the debugger will kick in and highlight the current place in execution. You can hover the cursor over variables in the IDE to examine their values, view the current call-stack (to see here this code has been called from) and get a list of local variables to help track program state. You can modify variable properties here too which often makes debugging simpler.

Execute code line by line by 'Stepping Over' (cmd+shift+o), which executes the current line, 'Stepping Into' (cmd_shift+i) which steps into the current line of code (if it is a function), or 'Stepping Out' to return back up the call stack.

If you want to stick to 'old-school' printf style debugging, go with NSLoging output to console.

NSLog(@"this text appears");

prints the following to the console:

this text appears

To print some basic variable values:

CGFloat pi = 3.14;
NSString *aFloatString = [NSString stringWithFormat:@"%.2f", pi];
NSLog(@"pi is equal to: %@", aFloatString);

Prints:

pi is equa to: 3.14

Standard c formatters can be used in NSLog i.e %d for int, %.2f for a float to 2 decimal places etc. Use %@ for NSString*s.

Remember that NSLog will remain in production code unless you #IFDEF it out of release builds (or something similar), so if you don't want a performance hit, or embarrassing console logs to accompany the app you will want to remove them.

I've been known to litter functions that dump the following to console - and it isn't good:

OUTPUT:
Number of vertices is: 1200
<Requested reduction>
Can I kick it?
....
....
YES. I. CAN!
Number of vertices is: 800

Could have done with removing things like that :|

davbryn
I just started in ios dev, whenever I hover, all I see is the address of the variable. How do you see the actual value of lets say NSString* ? Can I modifty it and how?
pdiddy
Click he 'gdb' button ?(looks like a small console) and look for the part of the window listing local variables (a small table). You may need to expand the 'Locals' header under 'Variable'. The 'Value' Column gives the memory address (or value for basics like int). 'Summary' will show you the string held in an NSString*, but you can't modify a summary AFAIK (as this is a representation of the object). Right-click (ctrl-left click) to get a context menu to help explore this window further.
davbryn
sorry, complete xCode newbie. How do access the IDE when I insert a breakpoint and click 'build and debug'?
sadmicrowave
@sadmicrowave: not exactly sure what you mean. The "build and debug" button is in the IDE. If you mean "debugger window", open Run->Debugger. You can also use the ["Page" button in the toolbar](http://imgur.com/lUCxb.png), which should be on the far left (click on the [bug spray](http://imgur.com/ODy8G.png) to switch to the debugger). See also ["Xcode Single Window Mode"](http://iphonedevelopment.blogspot.com/2009/03/xcode-single-window-mode.html).
outis