欢迎来到代码驿站!

iOS代码

当前位置:首页 > 移动开发 > iOS代码

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

时间:2021-05-31 08:24:13|栏目:iOS代码|点击:

问题描述

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应用是否开放HTTP权限的方法

栏    目:iOS代码

下一篇:iOS和JS交互教程之WKWebView-协议拦截详解

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

本文地址:http://www.codeinn.net/misctech/132280.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有