这篇文章主要介绍了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格式化工具中使用
感谢你能够认真阅读完这篇文章,希望小编分享的“iOS中Xcode9的Log日志无法输出中文怎么办”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。