一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

2021-04-05 15:42kuangdacaikuang IOS

這篇文章主要介紹了iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié),需要的朋友可以參考下

問題描述

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

效果

iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

而且直接就是json串,復(fù)制log日志,直接在json格式化工具中使用

 

iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)

參考

jllogencoding

總結(jié)

以上所述是小編給大家介紹的ios中解決xcode9的log日志無法輸出中文的問題小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/kuangdacaikuang/article/details/78496549

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久精品嫩草影院免费看 | 亚洲第一色网站 | 91亚洲精品国产自在现线 | 久久中文字幕无线观看 | 白丝爆动漫羞羞动漫软件 | 午夜影院和视费x看 | 欧美色图日韩 | 精品国产免费久久久久久 | 欧美日韩国产在线人成 | 欧美日韩视频在线成人 | 日韩成人精品在线 | 午夜精品久久久久久久99 | 特级av毛片免费观看 | 高h射尿 | 99精品视频在线观看 | 精品免费tv久久久久久久 | 小草观看免费高清视频 | 青草青草伊人精品视频 | 潘甜甜在线观看 | 3d美女触手怪爆羞羞漫画 | 久久偷拍国2017 | 亚州笫一色惰网站 | 9总探花新品牛仔背带裤 | haodiaose在线精品免费观看 | 青草久久网 | 天堂8在线天堂资源在线 | 欧美添下面视频免费观看 | 日本一区二区视频在线观看 | 国产精品一二三 | 午夜想想爱午夜剧场 | 爱爱小说漫画 | 天天操天天爽天天射 | 国产精品思瑞在线观看 | 99热这里只有精品在线观看 | 希望影院高清免费观看视频 | 成人性色生活片免费网 | 无码AV免费精品一区二区三区 | 翁息肉小说老扒 | a∨79成人网| 亚洲免费色 | 国产精品网站在线观看 |