So on twitter today a friend was complaining about Xcode always breaking in
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
instead of someplace useful for any time something went wrong.
The issue here is that “something going wrong” is often an exception being thrown, like what happens if you do this:
NSMutableDictionary * d = [NSMutableDictionary dictionaryWithCapacity: 10]; [d setObject: nil forKey: @"nope!"];
You cannot put a
nil in for the value of an
NSDictionary and so you get an exception thrown as you can see from the debug console output:
2012-08-17 18:05:29.781 test3434[66215:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil value (key: nope!)'
What this friend quite reasonably wanted was for the debugger to stop at that line where we tried to put
nil into the
NSDictionary. Here’s how you make that happen (I’m on Xcode 4.3.2 on this computer so pardon if the screenshots are different than for your Xcode version).
First create a breakpoint by switching to the breakpoints navigator and selecting the “+” icon at the lower left and then choose “Add Symbolic Breakpoint” from the menu that comes up and then enter “objc_exception_throw” for the symbol as shown:
After selecting “Done” to create the breakpoint you’ll see it in the breakpoints list.
Update: Two people pointed out that it might be easier to select “Add Exception Breakpoint…” from the “+” button’s pop-up menu and then configure it to be only ObjectiveC exceptions. This produces the same resulting breakpoint (confirm by creating both types and then using “br li” in the debugger console to list breakpoints).
Note that in either case the created breakpoint is under the project and we really want this to be a global breakpoint. Change that by right-clicking on the breakpoint to open the contextual menu:
And select “User” from the “Move Breakpoint To” menu’s submenu. You’ll then see that the “objc_exception_throw” breakpoint is listed under “User” instead of the project icon/name.
Run your test program with the bad code above and you should see it break exactly where you want it to (unless you have some other exception being thrown, of course).