问题描述
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
|
效果
而且直接就是json串,复制log日志,直接在json格式化工具中使用
参考
jllogencoding
总结
以上所述是小编给大家介绍的ios中解决xcode9的log日志无法输出中文的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!
原文链接:http://blog.csdn.net/kuangdacaikuang/article/details/78496549
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 49
-
2025-05-25 31
-
详解Spring Cloud Stream使用延迟消息实现定时任务(RabbitMQ)
2025-05-29 43 -
使用自助建站微信小程序时,怎样选择合适的模板以匹配品牌形象?
2025-06-04 78 -
2025-05-29 88
热门评论