|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Xcode 控制台输出中文 |
| 4 | +subtitle: 在 Xcode 控制台输出中文的方法 |
| 5 | +date: 2017-03-01 |
| 6 | +author: BY |
| 7 | +header-img: img/post-bg-cook.jpg |
| 8 | +catalog: true |
| 9 | +tags: |
| 10 | + - iOS |
| 11 | + - Xcode |
| 12 | + - macOS |
| 13 | +--- |
| 14 | + |
| 15 | +> 重写 Array、Set、Dictionary 的输出方法,实现在中文(Unicode)字符在控制台的输出 |
| 16 | +
|
| 17 | +## 原理 |
| 18 | + Xcode 控制台中在输出 Array、Set、Dictionary 时,其中的中文字符会变成Unicode编码 如`"\U67cf\U8367"`,重写输出方法 |
| 19 | + |
| 20 | + - (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level |
| 21 | + |
| 22 | +## 效果 |
| 23 | +先看看效果: |
| 24 | + |
| 25 | +创建一个字典并输出: |
| 26 | + |
| 27 | + NSData *strData = [@"str -> data格式的字符串" dataUsingEncoding:NSUTF8StringEncoding]; |
| 28 | + |
| 29 | + NSData *dicData = [NSJSONSerialization dataWithJSONObject:@{@"key0": @"字典 -> data 的数据",} |
| 30 | + options:NSJSONWritingPrettyPrinted |
| 31 | + error:nil]; |
| 32 | + |
| 33 | + NSMutableSet *set = [NSMutableSet setWithArray:@[@"set0", |
| 34 | + strData, |
| 35 | + dicData]]; |
| 36 | + NSDictionary *dic = @{@"name" : @"BY", |
| 37 | + @"My bolg" : @"http://qiubaiying.top", |
| 38 | + @"count" : @(11), |
| 39 | + @"strData" : strData, |
| 40 | + @"dicData" : dicData, |
| 41 | + @"set" : set, |
| 42 | + @"Unicode" : @"😀😁🤣😂😄", |
| 43 | + @"contact" : @[@"BY Blog:http://qiubaiying.top", |
| 44 | + @"GitHub:https://github.com/qiubaiying", |
| 45 | + @"简书:https://http://www.jianshu.com/u/e71990ada2fd"]}; |
| 46 | + NSLog(@"%@", dic); |
| 47 | + |
| 48 | +输出结果: |
| 49 | + |
| 50 | + 2017-03-01 10:36:45.709 BYFoundationLog_Demo[1657:53604] { |
| 51 | + "My bolg" = "http://qiubaiying.top"; |
| 52 | + Unicode = "\Ud83d\Ude00\Ud83d\Ude01\Ud83e\Udd23\Ud83d\Ude02\Ud83d\Ude04"; |
| 53 | + contact = ( |
| 54 | + "BY Blog:http://qiubaiying.top", |
| 55 | + "GitHub:https://github.com/qiubaiying", |
| 56 | + "\U7b80\U4e66:https://http://www.jianshu.com/u/e71990ada2fd" |
| 57 | + ); |
| 58 | + count = 11; |
| 59 | + dicData = <7b0a2020 226b6579 3022203a 2022e5ad 97e585b8 202d3e20 64617461 20e79a84 e695b0e6 8dae220a 7d>; |
| 60 | + name = BY; |
| 61 | + set = "{(\n <73747220 2d3e2064 617461e6 a0bce5bc 8fe79a84 e5ad97e7 aca6e4b8 b2>,\n set0,\n <7b0a2020 226b6579 3022203a 2022e5ad 97e585b8 202d3e20 64617461 20e79a84 e695b0e6 8dae220a 7d>\n)}"; |
| 62 | + strData = <73747220 2d3e2064 617461e6 a0bce5bc 8fe79a84 e5ad97e7 aca6e4b8 b2>; |
| 63 | + } |
| 64 | + |
| 65 | +将`BYFoundationLog.m`拖入项目,再次运行 |
| 66 | + |
| 67 | + 2017-03-01 10:35:52.545 BYFoundationLog_Demo[1635:52772] { |
| 68 | + set = {( |
| 69 | + "str -> data格式的字符串", |
| 70 | + "set0", |
| 71 | + { |
| 72 | + key0 = "字典 -> data 的数据", |
| 73 | + }, |
| 74 | + )}, |
| 75 | + Unicode = "😀😁🤣😂😄", |
| 76 | + strData = "str -> data格式的字符串", |
| 77 | + count = 11, |
| 78 | + dicData = { |
| 79 | + key0 = "字典 -> data 的数据", |
| 80 | + }, |
| 81 | + contact = ( |
| 82 | + "BY Blog:http://qiubaiying.top", |
| 83 | + "GitHub:https://github.com/qiubaiying", |
| 84 | + "简书:https://http://www.jianshu.com/u/e71990ada2fd", |
| 85 | + ), |
| 86 | + name = "BY", |
| 87 | + My bolg = "http://qiubaiying.top", |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | +## 实现方法 |
| 92 | + |
| 93 | +以 `NSArray` 为例: |
| 94 | + |
| 95 | +创建一个 `NSArray` 的分类,重写输出方法 |
| 96 | + |
| 97 | +``` |
| 98 | +@implementation NSArray (Log) |
| 99 | +
|
| 100 | +- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level { |
| 101 | + NSMutableString *desc = [NSMutableString string]; |
| 102 | + |
| 103 | + NSMutableString *tabString = [[NSMutableString alloc] initWithCapacity:level]; |
| 104 | + for (NSUInteger i = 0; i < level; ++i) { |
| 105 | + [tabString appendString:@"\t"]; |
| 106 | + } |
| 107 | + |
| 108 | + NSString *tab = @""; |
| 109 | + if (level > 0) { |
| 110 | + tab = tabString; |
| 111 | + } |
| 112 | + [desc appendString:@"\t(\n"]; |
| 113 | + |
| 114 | + for (id obj in self) { |
| 115 | + if ([obj isKindOfClass:[NSDictionary class]] |
| 116 | + || [obj isKindOfClass:[NSArray class]] |
| 117 | + || [obj isKindOfClass:[NSSet class]]) { |
| 118 | + NSString *str = [((NSDictionary *)obj) descriptionWithLocale:locale indent:level + 1]; |
| 119 | + [desc appendFormat:@"%@\t%@,\n", tab, str]; |
| 120 | + } else if ([obj isKindOfClass:[NSString class]]) { |
| 121 | + [desc appendFormat:@"%@\t\"%@\",\n", tab, obj]; |
| 122 | + } else if ([obj isKindOfClass:[NSData class]]) { |
| 123 | + |
| 124 | + NSError *error = nil; |
| 125 | + NSObject *result = [NSJSONSerialization JSONObjectWithData:obj |
| 126 | + options:NSJSONReadingMutableContainers |
| 127 | + error:&error]; |
| 128 | + |
| 129 | + if (error == nil && result != nil) { |
| 130 | + if ([result isKindOfClass:[NSDictionary class]] |
| 131 | + || [result isKindOfClass:[NSArray class]] |
| 132 | + || [result isKindOfClass:[NSSet class]]) { |
| 133 | + NSString *str = [((NSDictionary *)result) descriptionWithLocale:locale indent:level + 1]; |
| 134 | + [desc appendFormat:@"%@\t%@,\n", tab, str]; |
| 135 | + } else if ([obj isKindOfClass:[NSString class]]) { |
| 136 | + [desc appendFormat:@"%@\t\"%@\",\n", tab, result]; |
| 137 | + } |
| 138 | + } else { |
| 139 | + @try { |
| 140 | + NSString *str = [[NSString alloc] initWithData:obj encoding:NSUTF8StringEncoding]; |
| 141 | + if (str != nil) { |
| 142 | + [desc appendFormat:@"%@\t\"%@\",\n", tab, str]; |
| 143 | + } else { |
| 144 | + [desc appendFormat:@"%@\t%@,\n", tab, obj]; |
| 145 | + } |
| 146 | + } |
| 147 | + @catch (NSException *exception) { |
| 148 | + [desc appendFormat:@"%@\t%@,\n", tab, obj]; |
| 149 | + } |
| 150 | + } |
| 151 | + } else { |
| 152 | + [desc appendFormat:@"%@\t%@,\n", tab, obj]; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + [desc appendFormat:@"%@)", tab]; |
| 157 | + |
| 158 | + return desc; |
| 159 | +} |
| 160 | +
|
| 161 | +@end |
| 162 | +
|
| 163 | +``` |
| 164 | + |
| 165 | +NSSet、NSDictionary 与 NSArray 实现方法类似 |
| 166 | + |
| 167 | +## 代码下载 |
| 168 | + |
| 169 | +代码及Demo地址:[GitHub](https://github.com/qiubaiying/BYFoundationLog) |
| 170 | + |
| 171 | +## 使用方法 |
| 172 | + |
| 173 | +直接将 `BYFoundationLog.m` 文件拖入项目中就能使用 |
| 174 | + |
| 175 | + |
0 commit comments