2011年12月1日星期四

Objective-C Expressions for Debugging

Objective-C Expressions for Debugging:

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]);


没有评论:

发表评论