Four Methods for Debugging Objective-C

This post covers four methods that are available to assist with debugging Objective-C, each providing information about the current application context. The first three of four are shown in the example below:

- (void)buttonPressed:(UIButton *)button
  NSLog(@"Current selector: %@", NSStringFromSelector(_cmd));
  NSLog(@"Object class: %@", NSStringFromClass([self class]));
  NSLog(@"Filename: %@", [[NSString stringWithUTF8String:__FILE__] lastPathComponent]);

Debugging Objective-C

The above provides information about the selector that was called, in what class and the associated Objective-C source file (.m).

The fourth method available in Objective-C creates an array, where each entry is a string representing a value in the current stack trace. Here is how to print the stack trace:

NSLog(@"Stack trace: %@", [NSThread callStackSymbols]);

Methods for Debugging Objective-C

A stack trace can be quite helpful if you need to follow the flow of application logic to figure out where an error occurred.

  1. You can also use “po [NSThread callStackSymbols]” in the Xcode console. This is especially useful after EXE_BAD_ACCESS, SIGABRT and similar crashes that do not automatically write the call stack and exception description to the console.

Comments are closed.