IOS

关注公众号 jb51net

关闭
首页 > 软件编程 > IOS > ios解决xcode9的log日志无法输入中文

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

作者:kuangdacaikuang

这篇文章主要介绍了iOS中解决Xcode9的Log日志无法输出中文的问题小结,需要的朋友可以参考下

问题描述

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

效果

这里写图片描述

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

这里写图片描述

这里写图片描述

参考

JLLogEncoding

总结

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

您可能感兴趣的文章:
阅读全文