問題描述
xcode的log日志輸出中文的問題,一般都是重寫nsarray,nsdictionary的- (nsstring *)descriptionwithlocale:(id)locale;
方法進(jìn)行處理,最近升級(jí)到xcode9會(huì)后發(fā)現(xiàn)原來的處理邏輯也無法滿足輸出中文的需求,后臺(tái)返回的狀態(tài)描述涉及到中文的都變成了unicode編碼,其實(shí)這是重寫的方法失效的問題,因?yàn)閤code默認(rèn)輸出nsarray,nsdictionary的中文都是unicode編碼
正確的解決方案如下, 創(chuàng)建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]; } /** 將數(shù)組轉(zhuǎn)化成字符串,文字格式utf8,并且格式化 @param level 當(dāng)前數(shù)組的層級(jí),最少為 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; } /** 根據(jù)層級(jí),返回前面的空格占位符 @param level 層級(jí) @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]; } /** * 非字典時(shí),會(huì)引發(fā)崩潰 */ - (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; } /** 將字典轉(zhuǎn)化成字符串,文字格式utf8,并且格式化 @param level 當(dāng)前字典的層級(jí),最少為 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; } /** 根據(jù)層級(jí),返回前面的空格占位符 @param level 字典的層級(jí) @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串,復(fù)制log日志,直接在json格式化工具中使用
參考
總結(jié)
以上所述是小編給大家介紹的ios中解決xcode9的log日志無法輸出中文的問題小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/kuangdacaikuang/article/details/78496549