Coming from a Flex-Flash IDE, I was able to set breakpoints in my code and at runtime view the values of my variables in the corresponding window.
Now, I figured out where I can find the ‘variables’ window in Xcode and I can see all my variables there BUT the values of those variables can’t be seen. All I have is the data type and a bunch of hex numbers (pointers?).
How am I supposed to debug my code? Where can I see the values of my variables without having to log them in my code?
I am trying to see the values of a NSDictionary with a bunch of key/value pairs. But also any other NSObject for that matter. I have read something about overriding the description method but what about native objects?
Debugging with GDB
XCode gives you a GDB Debugger, so like Jano said in his comment, you can use GDB commands like po (print object) to view an object.
To print primitives like int, float, you can use
print,p, andpx(to view the number in hex)You can also see the result of running commands. For example, to view a string length you could do:
If you do not cast the return to an int, I believe you’ll see some complaining in the console.
To view a UIView’s frame (CGRect struct type), you can do:
Lastly, if you override the
descriptionmethod of a class, you can customize how it displays when written to the console or even to an NSLog for that matter. If you do[NSString stringWithFormat:@"My object... %@", myObj]the description method of that object will be called.Another good read is How to set a conditional breakpoint in Xcode based on an object string property?
Log Tip
If you want NSLog messages but only in debug builds, you might like the
DLogmacro we use at my work:It works just like NSLog except that it is compiled out on non-DEBUG builds. NSLog can actually be a performance hit, plus you may not want some messages spilling out in your logs.
We put this macro in the precompiled header file (MyApp-Prefix.pch) so that it gets included in all the project files.
Dumping Variables
Your comment asked about how to dump all variables of an object without writing code. I know of no built in way to do this. However, you might try using reflection. I have an implementation that will allow you to do something like:
You can make a category on NSObject to add a method to all NSObject types that will dump the information you’re after. I borrowed code from Objective C Introspection/Reflection to start off the code, but added code to include property values.
NSObject (DebuggingAid) category: