其他Ios@IONICiOS分享的demo

iOS JSON数据NSLog小技巧

2016-07-26  本文已影响3761人  朱晓辉

一.前言

说到JSON数据NSLog输出,大家可能并不陌生,在向服务器数据请求时,会经常用到,但Xcode的JSON输出并不怎么美丽,

二.先看效果

1.Xocde JSON数据正常NSLog输出,和copy到JOSN在线格式化工具情况如下:

image

2.添加扩展后.NSLog输出和JSON数据格式化效果如下

image

3.效果很明显了:

三.好了,不多说,直接上代码

1.新建一个NSdictionasy 的Category,笔者取名为NSDictionary+XHLogHelper,如下:
image
image
2.打开 NSDictionary+XHLogHelper.m 文件,添加代码如下:
#import "NSDictionary+XHLogHelper.h"

@implementation NSDictionary (XHLogHelper)

#if DEBUG
- (NSString *)descriptionWithLocale:(nullable id)locale{

  NSString *logString;

    @try {

        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;
}
#endif

@end

3.大功告成了
4.Tips
#ifdef DEBUG
#define DebugLog(...) NSLog(__VA_ARGS__)
#else
#define DebugLog(...)
#endif

四.小结:

上一篇 下一篇

猜你喜欢

热点阅读