欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS中解决Xcode9的Log日志无法输出中文的问题小结

程序员文章站 2023-12-20 10:38:52
问题描述 xcode的log日志输出中文的问题,一般都是重写nsarray,nsdictionary的- (nsstring *)descriptionwithlocal...

问题描述

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

正确的解决方案如下, 创建nsarray+zylog分类

nsarray+zylog.h文件

//
// 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文件

//
// 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];
}
/**
 将数组转化成字符串,文字格式utf8,并且格式化
 @param level 当前数组的层级,最少为 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;
}
/**
 根据层级,返回前面的空格占位符
 @param level 层级
 @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];
}
/**
 * 非字典时,会引发崩溃
 */
- (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;
}
/**
 将字典转化成字符串,文字格式utf8,并且格式化
 @param level 当前字典的层级,最少为 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;
}
/**
 根据层级,返回前面的空格占位符
 @param level 字典的层级
 @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日志无法输出中文的问题小结

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

iOS中解决Xcode9的Log日志无法输出中文的问题小结

iOS中解决Xcode9的Log日志无法输出中文的问题小结

参考

jllogencoding

总结

以上所述是小编给大家介绍的ios中解决xcode9的log日志无法输出中文的问题小结,希望对大家有所帮助

上一篇:

下一篇: