当前位置:首页 > 通信资讯 > 正文

问题描述

xcode的log日志输出中文的问题,一般都是重写nsarray,nsdictionary的- (nsstring *)descriptionwithlocale:(id)locale;方法进行处理,最近升级到xcode9会后发现原来的处理逻辑也无法满足输出中文的需求,后台返回的状态描述涉及到中文的都变成了unicode编码,其实这是重写的方法失效的问题,因为xcode默认输出nsarray,nsdictionary的中文都是unicode编码

正确的解决方案如下, 创建nsarray+zylog分类

nsarray+zylog.h文件

?
1 2 3 4 5 6 7 8 9 10 11 12 // // nsarray+zylog.h // zylog // // created by zhouyu on 17/11/08. // copyright © 2017年 zhouyu. all rights reserved. // #import <foundation/foundation.h> @interface nsarray (zylog) @end @interface nsdictionary (zylog) @end

nsarray+zylog.m文件

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 // // nsarray+zylog.m // zylog // // created by zhouyu on 17/11/08. // copyright © 2017年 zhouyu. all rights reserved. // #import "nsarray+zylog.h" @implementation nsarray (zylog) #ifdef debug - (nsstring *)description { return [self zy_descriptionwithlevel:1]; } -(nsstring *)descriptionwithlocale:(id)locale{ return [self zy_descriptionwithlevel:1]; } - (nsstring *)descriptionwithlocale:(nullable id)locale indent:(nsuinteger)level { return [self zy_descriptionwithlevel:(int)level]; } /** 将数组转化成字符串,文字格式utf8,并且格式化 @param level 当前数组的层级,最少为 1,代表最外层 @return 格式化的字符串 */ - (nsstring *)zy_descriptionwithlevel:(int)level { nsstring *subspace = [self zy_getspacewithlevel:level]; nsstring *space = [self zy_getspacewithlevel:level - 1]; nsmutablestring *retstring = [[nsmutablestring alloc] init]; // 1、添加 [ [retstring appendstring:[nsstring stringwithformat:@"["]]; // 2、添加 value [self enumerateobjectsusingblock:^(id _nonnull obj, nsuinteger idx, bool * _nonnull stop) { if ([obj iskindofclass:[nsstring class]]) { nsstring *value = (nsstring *)obj; value = [value stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; nsstring *substring = [nsstring stringwithformat:@"\n%@\"%@\",", subspace, value]; [retstring appendstring:substring]; } else if ([obj iskindofclass:[nsarray class]]) { nsarray *arr = (nsarray *)obj; nsstring *str = [arr zy_descriptionwithlevel:level + 1]; str = [nsstring stringwithformat:@"\n%@%@,", subspace, str]; [retstring appendstring:str]; } else if ([obj iskindofclass:[nsdictionary class]]) { nsdictionary *dic = (nsdictionary *)obj; nsstring *str = [dic descriptionwithlocale:nil indent:level + 1]; str = [nsstring stringwithformat:@"\n%@%@,", subspace, str]; [retstring appendstring:str]; } else { nsstring *substring = [nsstring stringwithformat:@"\n%@%@,", subspace, obj]; [retstring appendstring:substring]; } }]; if ([retstring hassuffix:@","]) { [retstring deletecharactersinrange:nsmakerange(retstring.length-1, 1)]; } // 3、添加 ] [retstring appendstring:[nsstring stringwithformat:@"\n%@]", space]]; return retstring; } /** 根据层级,返回前面的空格占位符 @param level 层级 @return 占位空格 */ - (nsstring *)zy_getspacewithlevel:(int)level { nsmutablestring *mustr = [[nsmutablestring alloc] init]; for (int i=0; i<level; i++) { [mustr appendstring:@"\t"]; } return mustr; } #endif @end @implementation nsdictionary (zylog) #ifdef debug - (nsstring *)description { return [self zy_descriptionwithlevel:1]; } - (nsstring *)descriptionwithlocale:(nullable id)locale { return [self zy_descriptionwithlevel:1]; } - (nsstring *)descriptionwithlocale:(nullable id)locale indent:(nsuinteger)level { return [self zy_descriptionwithlevel:(int)level]; } /** * 非字典时,会引发崩溃 */ - (nsstring *)zy_getutf8string { if ([self iskindofclass:[nsdictionary class]] == no) { return @""; } nserror *error = nil; nsdata *data = [nsjsonserialization datawithjsonobject:self options:nsjsonwritingprettyprinted error:&error]; if (error) { return @""; } nsstring *str = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; return str; } /** 将字典转化成字符串,文字格式utf8,并且格式化 @param level 当前字典的层级,最少为 1,代表最外层字典 @return 格式化的字符串 */ - (nsstring *)zy_descriptionwithlevel:(int)level { nsstring *subspace = [self zy_getspacewithlevel:level]; nsstring *space = [self zy_getspacewithlevel:level - 1]; nsmutablestring *retstring = [[nsmutablestring alloc] init]; // 1、添加 { [retstring appendstring:[nsstring stringwithformat:@"{"]]; // 2、添加 key : value; [self enumeratekeysandobjectsusingblock:^(id _nonnull key, id _nonnull obj, bool * _nonnull stop) { if ([obj iskindofclass:[nsstring class]]) { nsstring *value = (nsstring *)obj; value = [value stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding]; nsstring *substring = [nsstring stringwithformat:@"\n%@\"%@\" : \"%@\",", subspace, key, value]; [retstring appendstring:substring]; } else if ([obj iskindofclass:[nsdictionary class]]) { nsdictionary *dic = (nsdictionary *)obj; nsstring *str = [dic zy_descriptionwithlevel:level + 1]; str = [nsstring stringwithformat:@"\n%@\"%@\" : %@,", subspace, key, str]; [retstring appendstring:str]; } else if ([obj iskindofclass:[nsarray class]]) { nsarray *arr = (nsarray *)obj; nsstring *str = [arr descriptionwithlocale:nil indent:level + 1]; str = [nsstring stringwithformat:@"\n%@\"%@\" : %@,", subspace, key, str]; [retstring appendstring:str]; } else { nsstring *substring = [nsstring stringwithformat:@"\n%@\"%@\" : %@,", subspace, key, obj]; [retstring appendstring:substring]; } }]; if ([retstring hassuffix:@","]) { [retstring deletecharactersinrange:nsmakerange(retstring.length-1, 1)]; } // 3、添加 } [retstring appendstring:[nsstring stringwithformat:@"\n%@}", space]]; return retstring; } /** 根据层级,返回前面的空格占位符 @param level 字典的层级 @return 占位空格 */ - (nsstring *)zy_getspacewithlevel:(int)level { nsmutablestring *mustr = [[nsmutablestring alloc] init]; for (int i=0; i<level; i++) { [mustr appendstring:@"\t"]; } return mustr; } #endif @end

效果

ios中解决xcode9的log日志无法输出中文的(ios中解决xcode9的log日志无法输出中文的)

而且直接就是json串,复制log日志,直接在json格式化工具中使用

ios中解决xcode9的log日志无法输出中文的(ios中解决xcode9的log日志无法输出中文的)

ios中解决xcode9的log日志无法输出中文的(ios中解决xcode9的log日志无法输出中文的)

参考

jllogencoding

总结

以上所述是小编给大家介绍的ios中解决xcode9的log日志无法输出中文的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/kuangdacaikuang/article/details/78496549
如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。