查看: 445|回复: 0

[IOS开发教程] iOS中解决Xcode9的Log日志无法输出中文的问题小结

发表于 2017-11-29 08:00:04

问题描述

Xcode的Log日志输出中文的问题,一般都是重写NSArray,NSDictionary的- (NSString *)descriptionWithLocale:(id)locale;方法进行处理,最近升级到Xcode9会后发现原来的处理逻辑也无法满足输出中文的需求,后台返回的状态描述涉及到中文的都变成了Unicode编码,其实这是重写的方法失效的问题,因为Xcode默认输出NSArray,NSDictionary的中文都是Unicode编码

正确的解决方案如下, 创建NSArray+ZYLog分类

NSArray+ZYLog.h文件

  1. //
  2. // NSArray+ZYLog.h
  3. // ZYLog
  4. //
  5. // Created by zhouyu on 17/11/08.
  6. // Copyright ? 2017年 zhouyu. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface NSArray (ZYLog)
  10. @end
  11. @interface NSDictionary (ZYLog)
  12. @end
复制代码

NSArray+ZYLog.m文件

  1. //
  2. // NSArray+ZYLog.m
  3. // ZYLog
  4. //
  5. // Created by zhouyu on 17/11/08.
  6. // Copyright ? 2017年 zhouyu. All rights reserved.
  7. //
  8. #import "NSArray+ZYLog.h"
  9. @implementation NSArray (ZYLog)
  10. #ifdef DEBUG
  11. - (NSString *)description {
  12. return [self ZY_descriptionWithLevel:1];
  13. }
  14. -(NSString *)descriptionWithLocale:(id)locale{
  15. return [self ZY_descriptionWithLevel:1];
  16. }
  17. - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
  18. return [self ZY_descriptionWithLevel:(int)level];
  19. }
  20. /**
  21. 将数组转化成字符串,文字格式UTF8,并且格式化
  22. @param level 当前数组的层级,最少为 1,代表最外层
  23. @return 格式化的字符串
  24. */
  25. - (NSString *)ZY_descriptionWithLevel:(int)level {
  26. NSString *subSpace = [self ZY_getSpaceWithLevel:level];
  27. NSString *space = [self ZY_getSpaceWithLevel:level - 1];
  28. NSMutableString *retString = [[NSMutableString alloc] init];
  29. // 1、添加 [
  30. [retString appendString:[NSString stringWithFormat:@"["]];
  31. // 2、添加 value
  32. [self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  33. if ([obj isKindOfClass:[NSString class]]) {
  34. NSString *value = (NSString *)obj;
  35. value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  36. NSString *subString = [NSString stringWithFormat:@"\n%@\"%@\",", subSpace, value];
  37. [retString appendString:subString];
  38. } else if ([obj isKindOfClass:[NSArray class]]) {
  39. NSArray *arr = (NSArray *)obj;
  40. NSString *str = [arr ZY_descriptionWithLevel:level + 1];
  41. str = [NSString stringWithFormat:@"\n%@%@,", subSpace, str];
  42. [retString appendString:str];
  43. } else if ([obj isKindOfClass:[NSDictionary class]]) {
  44. NSDictionary *dic = (NSDictionary *)obj;
  45. NSString *str = [dic descriptionWithLocale:nil indent:level + 1];
  46. str = [NSString stringWithFormat:@"\n%@%@,", subSpace, str];
  47. [retString appendString:str];
  48. } else {
  49. NSString *subString = [NSString stringWithFormat:@"\n%@%@,", subSpace, obj];
  50. [retString appendString:subString];
  51. }
  52. }];
  53. if ([retString hasSuffix:@","]) {
  54. [retString deleteCharactersInRange:NSMakeRange(retString.length-1, 1)];
  55. }
  56. // 3、添加 ]
  57. [retString appendString:[NSString stringWithFormat:@"\n%@]", space]];
  58. return retString;
  59. }
  60. /**
  61. 根据层级,返回前面的空格占位符
  62. @param level 层级
  63. @return 占位空格
  64. */
  65. - (NSString *)ZY_getSpaceWithLevel:(int)level {
  66. NSMutableString *mustr = [[NSMutableString alloc] init];
  67. for (int i=0; i<level; i++) {
  68. [mustr appendString:@"\t"];
  69. }
  70. return mustr;
  71. }
  72. #endif
  73. @end
  74. @implementation NSDictionary (ZYLog)
  75. #ifdef DEBUG
  76. - (NSString *)description {
  77. return [self ZY_descriptionWithLevel:1];
  78. }
  79. - (NSString *)descriptionWithLocale:(nullable id)locale {
  80. return [self ZY_descriptionWithLevel:1];
  81. }
  82. - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
  83. return [self ZY_descriptionWithLevel:(int)level];
  84. }
  85. /**
  86. * 非字典时,会引发崩溃
  87. */
  88. - (NSString *)ZY_getUTF8String {
  89. if ([self isKindOfClass:[NSDictionary class]] == NO) {
  90. return @"";
  91. }
  92. NSError *error = nil;
  93. NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
  94. if (error) {
  95. return @"";
  96. }
  97. NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  98. return str;
  99. }
  100. /**
  101. 将字典转化成字符串,文字格式UTF8,并且格式化
  102. @param level 当前字典的层级,最少为 1,代表最外层字典
  103. @return 格式化的字符串
  104. */
  105. - (NSString *)ZY_descriptionWithLevel:(int)level {
  106. NSString *subSpace = [self ZY_getSpaceWithLevel:level];
  107. NSString *space = [self ZY_getSpaceWithLevel:level - 1];
  108. NSMutableString *retString = [[NSMutableString alloc] init];
  109. // 1、添加 {
  110. [retString appendString:[NSString stringWithFormat:@"{"]];
  111. // 2、添加 key : value;
  112. [self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  113. if ([obj isKindOfClass:[NSString class]]) {
  114. NSString *value = (NSString *)obj;
  115. value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  116. NSString *subString = [NSString stringWithFormat:@"\n%@\"%@\" : \"%@\",", subSpace, key, value];
  117. [retString appendString:subString];
  118. } else if ([obj isKindOfClass:[NSDictionary class]]) {
  119. NSDictionary *dic = (NSDictionary *)obj;
  120. NSString *str = [dic ZY_descriptionWithLevel:level + 1];
  121. str = [NSString stringWithFormat:@"\n%@\"%@\" : %@,", subSpace, key, str];
  122. [retString appendString:str];
  123. } else if ([obj isKindOfClass:[NSArray class]]) {
  124. NSArray *arr = (NSArray *)obj;
  125. NSString *str = [arr descriptionWithLocale:nil indent:level + 1];
  126. str = [NSString stringWithFormat:@"\n%@\"%@\" : %@,", subSpace, key, str];
  127. [retString appendString:str];
  128. } else {
  129. NSString *subString = [NSString stringWithFormat:@"\n%@\"%@\" : %@,", subSpace, key, obj];
  130. [retString appendString:subString];
  131. }
  132. }];
  133. if ([retString hasSuffix:@","]) {
  134. [retString deleteCharactersInRange:NSMakeRange(retString.length-1, 1)];
  135. }
  136. // 3、添加 }
  137. [retString appendString:[NSString stringWithFormat:@"\n%@}", space]];
  138. return retString;
  139. }
  140. /**
  141. 根据层级,返回前面的空格占位符
  142. @param level 字典的层级
  143. @return 占位空格
  144. */
  145. - (NSString *)ZY_getSpaceWithLevel:(int)level {
  146. NSMutableString *mustr = [[NSMutableString alloc] init];
  147. for (int i=0; i<level; i++) {
  148. [mustr appendString:@"\t"];
  149. }
  150. return mustr;
  151. }
  152. #endif
  153. @end
复制代码

效果

这里写图片描述

而且直接就是json串,复制log日志,直接在json格式化工具中使用

这里写图片描述

这里写图片描述

参考

JLLogEncoding

总结

以上所述是小编给大家介绍的iOS中解决Xcode9的Log日志无法输出中文的问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报

关闭

站长推荐上一条 /1 下一条