ios中使用宏定义进行调试

第一种,在控制台上输出日志信息:

#ifdef DEBUG
#define DLog(format,...) NSLog((@"DLog %s - [Line %d] %s\n\n" format), __PRETTY_FUNCTION__,__LINE__, __FUNCTION__,##__VA_ARGS__)
#else
#define DLog(format,...) do {} while(0)
#endif

让NSLog只在debug build的时候起作用。将这个功能添加到全局都能访问得到的头文件中。这样你就可以尽情的使用log了,并且当进行production时,不会包含log相关代码。

第二种,定义AlertView控制输出:

#define ShowAlert(format, ...) myShowAlert(__LINE__, (char *)__FUNCTION__, format, ##__VA_ARGS__)
void myShowAlert(int line, char *functName, id formatstring,...){
    va_list arglist;
    if (!formatstring) {
        return;
    }
    va_start(arglist, formatstring);
    id outString = [[NSString alloc] initWithFormat:formatstring arguments:arglist];
    va_end(arglist);
    
    NSString *fileName = [[NSString stringWithCString:__FILE__ encoding:NSUTF8StringEncoding] lastPathComponent];
    
    NSString *defugInfo = [NSString stringWithFormat:@"file=%@ line=%d\n%s",fileName,line,functName];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:outString message:defugInfo delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。