7

I'm still haven't properly learned how to use the Xcode debugger, but I was wondering if anyone has some favourite debugging tips, things you can quickly insert into code to see the state of objects. Anything which would help me get more of a grasp on the internals of Objective-c.

Mostly I rely on NSLog(@"%@", myObject) to see what's happening with myObject, or sometimes NSLog(@"%@", [myObject class]) to check that something is really the class it should be. I know that I can do both by using the debugger, but I want to try using code for the moment, before I take the leap into using a full debugger.

Do you have any similar tricks?

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Don't put everything on one line. If you access an index inside a dictionary inside an instance of a custom class, make each level of access a separate temporary, vs "daisy chaining" everything together. Easier to maintain and easier to debug, since you can see all the intermediate values with the debugger. – Hot Licks Jul 19 '13 at 15:53
  • In the console, when stopped at a breakpoint, type "help". It will show you the available debugger commands. Become familiar, especially, with "po", since it will reliably display stuff the variable display window won't. – Hot Licks Jul 19 '13 at 15:55

2 Answers2

3

Asserts. Lots and lots of asserts. When you assume something must be some way, assert that it is true.

Build & Analyze is the new Build. Use the Clang Static Analyzer in Snow Leopard.

There is no magic; everything on your system and in your code happens for a reason, including crashes & misbehavior.

Embrace the debugger; it is really powerful and quite easy to start using.

Greg Parker's weblog is a wonderful source for a "behind the curtains" view of how some things work: http://www.sealiesoftware.com/blog/

bbum
  • 162,346
  • 23
  • 271
  • 359
3

I also use a lot of NSAsserts all around my code.
Here are 2 useful articles by Chris Hanson about NSAssert:

Another debugging technique I use often:
As Xcode does not display array contents in the debugger view, you can use the (gdb) console or the expression window to list array contents. Here is a related SO post.

Community
  • 1
  • 1
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112