開源HYBUnicodeReadable日志顯示Unicode中文
前言
開發中經常需要打印日志以查看數據是否正確,或者說查看數據的格式。但是,蘋果對于我們的 NSDictionary 、 NSSet 、 NSArray 等值有中文時,打印出來的是 Unicode 編碼,人類無法直接讀懂,因此,筆者研究研究如何將打印出來的日志保持原有的格式化且能夠將 Unicode 編碼打印出來是正常人類可讀懂的中文。
實現原理
蘋果給我們提供了本地化的方法,對于 NSDictionary 、 NSSet 、 NSArray 都可以重寫該方法來實現:
NSSet實現
對于 NSSet 實現如下:
- (NSString *)descriptionWithLocale:(id)localeindent:(NSUInteger)level {
NSMutableString *desc = [NSMutableString string];
NSMutableString *tabString = [[NSMutableString alloc]initWithCapacity:level];
for (NSUInteger i = 0; i < level; ++i) {
[tabStringappendString:@"\t"];
}
NSString *tab = @"\t";
if (level > 0) {
tab = tabString;
}
[descappendString:@"\t{(\n"];
for (id objin self) {
if ([objisKindOfClass:[NSDictionary class]]
|| [objisKindOfClass:[NSArray class]]
|| [objisKindOfClass:[NSSet class]]) {
NSString *str = [((NSDictionary *)obj)descriptionWithLocale:localeindent:level + 1];
[descappendFormat:@"%@\t%@,\n", tab, str];
} else if ([objisKindOfClass:[NSString class]]) {
[descappendFormat:@"%@\t\"%@\",\n", tab, obj];
} else {
[descappendFormat:@"%@\t%@,\n", tab, obj];
}
}
[descappendFormat:@"%@)}", tab];
return desc;
}
我們這里根本縮進級別來生成對應的格式化字符串,以便解決多級嵌套時格式不太美觀的問題。
NSArray實現
對于 NSArray 的實現如下:
- (NSString *)descriptionWithLocale:(id)localeindent:(NSUInteger)level {
NSMutableString *desc = [NSMutableString string];
NSMutableString *tabString = [[NSMutableString alloc]initWithCapacity:level];
for (NSUInteger i = 0; i < level; ++i) {
[tabStringappendString:@"\t"];
}
NSString *tab = @"";
if (level > 0) {
tab = tabString;
}
[descappendString:@"\t(\n"];
for (id objin self) {
if ([objisKindOfClass:[NSDictionary class]]
|| [objisKindOfClass:[NSArray class]]
|| [objisKindOfClass:[NSSet class]]) {
NSString *str = [((NSDictionary *)obj)descriptionWithLocale:localeindent:level + 1];
[descappendFormat:@"%@\t%@,\n", tab, str];
} else if ([objisKindOfClass:[NSString class]]) {
[descappendFormat:@"%@\t\"%@\",\n", tab, obj];
} else {
[descappendFormat:@"%@\t%@,\n", tab, obj];
}
}
[descappendFormat:@"%@)", tab];
return desc;
}
同樣的道理,不過筆者對于字符串值,都加上了雙引號,一眼就可以看出來是字符串類型。
NSDictionary實現
對于NSDictonary的實現如下:
- (NSString *)descriptionWithLocale:(id)localeindent:(NSUInteger)level {
NSMutableString *desc = [NSMutableString string];
NSMutableString *tabString = [[NSMutableString alloc]initWithCapacity:level];
for (NSUInteger i = 0; i < level; ++i) {
[tabStringappendString:@"\t"];
}
NSString *tab = @"";
if (level > 0) {
tab = tabString;
}
[descappendString:@"\t{\n"];
// 遍歷數組,self就是當前的數組
for (id keyin self.allKeys) {
id obj = [selfobjectForKey:key];
if ([objisKindOfClass:[NSString class]]) {
[descappendFormat:@"%@\t%@ = \"%@\",\n", tab, key, obj];
} else if ([objisKindOfClass:[NSArray class]]
|| [objisKindOfClass:[NSDictionary class]]
|| [objisKindOfClass:[NSSet class]]) {
[descappendFormat:@"%@\t%@ = %@,\n", tab, key, [objdescriptionWithLocale:localeindent:level + 1]];
} else {
[descappendFormat:@"%@\t%@ = %@,\n", tab, key, obj];
}
}
[descappendFormat:@"%@}", tab];
return desc;
}
字典打印出來的格式是{}這樣的結構,我們一樣要考慮嵌套情況,并且根據嵌套添加對應的級別縮進。
測試
我們來測試一下效果,我們打印如下一個字典:
NSMutableSet *set = [NSMutableSetsetWithArray:@[@"可變集合", @"字典->不可變集合->可變集合"]];
NSDictionary *dict = @{@"name" : @"標哥的技術博客",
@"title" : @"http://www.henishuo.com",
@"count" : @(11),
@"results" : [NSSetsetWithObjects:@"集合值1", @"集合值2", set , nil],
@"summaries" : @[@"sm1", @"sm2", @{@"keysm": @{@"stkey": @"字典->數組->字典->字典"}}],
@"parameters" : @{@"key1" : @"value1", @"key2": @{@"key11" : @"value11", @"key12" : @[@"三層", @"字典->字典->數組"]}},
@"hasBug": @[@"YES",@"NO"],
@"contact" : @[@"關注博客地址:http://www.henishuo.com", @"QQ群: 324400294", @"關注微博:標哥Jacky", @"關注GITHUB:CoderJackyHuang"]};
NSLog(@"%@", dict);
打印效果如下:
如何使用
首先要得到源代碼,安裝方式有兩種,一種是直接使用 Cocoapods :
pod 'HYBUnicodeReadable', '~> 0.0.1'
或者直接下載源代碼 https://github.com/CoderJackyHuang/HYBUnicodeReadable ,放入工程中即可
溫馨提示:不需要引入頭文件,即可達到效果!
寫在最后
如果在使用過程中出現任何bug,請反饋作者以便及時修正!謝謝您的支持!
如果覺得贊,請給一個star
關注我
如果在使用過程中遇到問題,或者想要與我交流,可加入有問必答 QQ群: 324400294
關注微信公眾號: iOSDevShares
關注新浪微博賬號:標哥Jacky
支持并捐助
如果您覺得文章對您很有幫助,希望得到您的支持。您的捐肋將會給予我最大的鼓勵,感謝您的支持!
| 支付寶捐助 | 微信捐助 |
|---|---|
|
|
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!