ios字典存bool_#iOS 打印中文字典,数组,控制台输出中文,并保持缩进格式
為了方便調試我們經常需要在控制臺打印數組/字典信息,但是如果含有中文,打印出來的就是一堆看不懂的信息(其實是Unicode編碼),影響開發效率.
本文目標:
使用NSLog能打印中文字典/數組
在控制臺使用 po 命令 顯示的調試信息也是中文的
2016-12-2 更新
支持對控件的打印,比如view.subviews
運用的是方法交換,只是對系統的debugDescription方法做轉碼處理,因此格式保留為系統風格
只在DEBUG模式下有效,對線上版本無干擾
效果:
用NSLog打印效果
控制臺使用po命令效果
如何使用
直接拖進項目中去即可.非常簡單.
解決NSLog打印中文問題
對于NSLog能打印中文字典/數組這個問題,首先看看網上的普通做法: 就是重新寫一個字典和數組的分類,重寫他們的- (NSString *)descriptionWithLocale:(id)locale這個方法
代碼如下:
@implementation NSDictionary (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *string = [NSMutableString string];
// 開頭有個{
[string appendString:@"{\n"];
// 遍歷所有的鍵值對
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOLBOOL *stop) {
[string appendFormat:@"\t%@", key];
[string appendString:@" : "];
[string appendFormat:@"%@,\n", obj];
}];
// 結尾有個}
[string appendString:@"}"];
// 查找最后一個逗號
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];
return string;
}
@end
@implementation NSArray (Log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *string = [NSMutableString string];
// 開頭有個[
[string appendString:@"[\n"];
// 遍歷所有的元素
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOLBOOL *stop) {
[string appendFormat:@"\t%@,\n", obj];
}];
// 結尾有個]
[string appendString:@"]"];
// 查找最后一個逗號
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];
return string;
} 性能呢
@end
```
這樣做可以解決問題,但是存在個問題:
* 打印出來的格式不規范,看起來別扭,括號,大括號位置基本沒法對上.
說說我的思路:基本上和上面的一致,需要重寫`- (NSString *)descriptionWithLocale:(id)locale`這個方法,不同的是,直接調用`self的description`方法,然后對返回的字符串進行處理,將其轉換為中文.
代碼如下:
給NSString寫的分類處理Unicode的編碼
```
- (NSString *)unicodeString{
NSString *tempStr1 = [self stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
NSPropertyListFormat format = NSPropertyListOpenStepFormat;
NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:&format error:nil];
return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
}
```
給NSDictionary和NSArray寫的分類中:
```
- (NSString *)descriptionWithLocale:(id)locale{
return self.description.unicodeString;
}
```
到此,用NSLog就能打印出規范的中文了,效果如下:

####解決控制臺調試命令`po`出來的不是中文問題:
方案和上面的基本差不多,能用系統的方法就用系統的,畢竟系統的穩定,速度快.
原理:使用'po'命令會調用`debugDescription`這個方法,這個返回調試環境下的信息,不建議重寫`description`這個方法.
代碼如下:
```
- (NSString *)descriptionWithLocale:(id)locale{
return self.description.unicodeString;
}
- (NSString *)debugDescription{
return self.description.unicodeString;
}
```
到此,使用`po`命令就可以查看包含中文的信息了.

附上最終github鏈接直接拖到項目中即可使用:
總結
以上是生活随笔為你收集整理的ios字典存bool_#iOS 打印中文字典,数组,控制台输出中文,并保持缩进格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 香蕉上为什么长斑点?
- 下一篇: 浪潮as5300技术方案_混闪存储AS5