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

来源:爱站网时间:2020-03-14编辑:网友分享
不管开发什么,我们都会使用日记来调试应用程序,这可以有效的解决程序崩溃的问题,其实也是程序员常用的手段,下面是爱站技术频道小编带给大家的iOS中解决Xcode9的Log日志无法输出中文的问题小结,一起来学习吧!

不管开发什么,我们都会使用日记来调试应用程序,这可以有效的解决程序崩溃的问题,其实也是程序员常用的手段,下面是爱站技术频道小编带给大家的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日志无法输出中文的问题小结的全部介绍都在这里了,相信小编的介绍对大家有一定的帮助,收藏js.aizhan.com,你绝对优先掌握很多学习的知识。

上一篇:iOS11 下载之断点续传的bug的解决方法

下一篇:iOS之单独使用UISearchBar创建搜索框的示例

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载