您好,登錄后才能下訂單哦!
這篇文章主要介紹了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。