控制台--NSLog输出

一:对返回的Json数据输出进行转化成中文状态输出

操作如下:

 对Json数据里的字典类型进行拓展转化,创建一个NSDictionary类 Category类型文件。
 #import <Foundation/Foundation.h>
 @interface NSDictionary (Log)

 @end

  #import "NSDictionary+Log.h"

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(nullable id)locale{
    
    NSString *logString;

    @try {
 //这里做一下处理判断,可以过滤一些输出造成莫名的闪退
        if (![locale isEqual:[NSNull alloc]]) { 
            logString=[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
        }
    } @catch (NSException *exception) {

        NSString *reason = [NSString stringWithFormat:@"reason:%@",exception.reason];
        logString = [NSString stringWithFormat:@"转换失败:\n%@,\n转换终止,输出如下:\n%@",reason,self.description];

    } @finally {
      
    }

    return logString;

 }
 @end


  使用  //data :返回来的数据
  NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
  NSLog(@"==data===%@",[dictionary descriptionWithLocale:dictionary[@"data"]]);

二:对输出进行定位监控

#ifdef DEBUG

//输出,定位到具体类,函数,代码行数
#define Log(format, ...)  fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

#else

#define Log(format, ...)  NSLog(format, ## __VA_ARGS__)

#endif

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。