Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
In a previous post I wrote about preprocessor macros that provide filename, line number and function information to aid in debugging. A short example follows:
- (void)buttonPressed:(UIButton *)button
{
NSLog(@"\n Function: %s\n Pretty function: %s\n Line: %d\n File: %s\n Object: %@",
__func__, __PRETTY_FUNCTION__, __LINE__, __FILE__, button);
...
}
The output from the above:

In addition to the macros, there are four Objective-C expressions which provides additional information about the current context in your code. Three of the four expressions 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]);
...
}

The fourth expression 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]);

Related posts:
- Objective-C Comment Styles
- Resize/Scale of an Image – Take 1 – Using an Objective-C Category
- Objective-C Object as a C Structure
Comments
4 Responses to “Objective-C Expressions for Debugging”
Leave Comment
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.
[Reply]
thank u!!!
[Reply]
thank you ………….
[Reply]
What is the +335, +71, +63 mean in the code?
Is there any way to get the line numbers or function names from the callstack??
[Reply]