iOS 开发实用技巧

Objective-C中的Debug表达式

2017-02-07  本文已影响1人  子斌

Objective-CC语言一样,提供了一些标准宏,描述了当前文件,所在源码文件的行数,以及函数信息。而Objective-C本身,也提供了相关的类类型。都可以应用在调试和错误处理日志当中。
预处理器在C/C++/Objective-C语言中提供的宏。

 %s  __func__ 当前函数名
 %d  __LINE__ 在源代码文件中当前所在行数
 %s  __FILE__ 当前源代码文件全路径   
 %s __PRETTY_FUNCTION__  像 __func__,但是包含了C++代码中的隐形类型信息。

在Objective-C使用的一些日志信息

 %@  NSStringFromSelector(_cmd)  当前selector名称
 %@  NSStringFromClass([self class]) 当前对象名
 %@  [[NSString stringWithUTF8String:**FILE**] lastPathComponent]源代码文件名
 %@  [NSThread callStackSymbols]  当前stack的可读字符串数组。仅在调度时使用。

示例代码:

#import <foundation /Foundation.h>  
  
@interface Person : NSObject  
- (void) run;  
@end  
  
@implementation Person  
- (void) run:{  
    NSLog(@"%s:%d:%s:%s", __func__, __LINE__, __FILE__,__PRETTY_FUNCTION__);  
    NSLog(@"%@",NSStringFromSelector(_cmd));  
    NSLog(@"%@",NSStringFromClass([self class]));  
    NSLog(@"%@",[[NSString stringWithUTF8String:__FILE__] lastPathComponent]);  
    NSLog(@"%@",[NSThread callStackSymbols]);  
    NSLog(@"%@",input);}  
 @end  
  
int main (int argc, const char * argv[]){  
    @autoreleasepool {  
        Person *p1 = [[Person alloc]init];  
        [p1 run];         
    }      
    return 0;  
}  
上一篇 下一篇

猜你喜欢

热点阅读