Summary of how to solve the problem that xcode9 log can’t output Chinese in IOS

Time:2021-2-26

Problem description

Xcode’s log output in Chinese is usually rewrittenNsarray, nsdictionary's - (nsstring *) descriptionwithlocale: (ID) locale;Method. Recently, after upgrading to xcode9, it was found that the original processing logic could not meet the requirement of outputting Chinese. The status description returned by the background involving Chinese has become Unicode encoding. In fact, this is the problem of invalid rewriting method, because the default output of Xcode is nsarray, and the Chinese of nsdictionary is Unicode encoding

The correct solution is as follows: create nsarray + zylog classification

Nsarray + zylog. H file

//
// NSArray+ZYLog.h
// ZYLog
//
// Created by zhouyu on 17/11/08.
//Copyright? 2017 Zhou. All rights reserved
//
#import <Foundation/Foundation.h>
@interface NSArray (ZYLog)
@end
@interface NSDictionary (ZYLog)
@end

Nsarray + zylog. M file

//
// NSArray+ZYLog.m
// ZYLog
//
// Created by zhouyu on 17/11/08.
//Copyright? 2017 Zhou. 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];
}
/**
 The array is converted to a string, the text format utf8, and formatted
 @Param level the level of the current array, at least 1, representing the outermost layer
 @Return the formatted string
 */
- (NSString *)ZY_descriptionWithLevel:(int)level {
 NSString *subSpace = [self ZY_getSpaceWithLevel:level];
 NSString *space = [self ZY_getSpaceWithLevel:level - 1];
 NSMutableString *retString = [[NSMutableString alloc] init];
 //1. Add[
 [retString appendString:[NSString stringWithFormat:@"["]];
 //2. Add 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. Add]
 [retString appendString:[NSString stringWithFormat:@"\n%@]", space]];
 return retString;
}
/**
 Returns the preceding space placeholder according to the hierarchy
 @Param level
 @Return space
 */
- (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];
}
/**
 *When it is not a dictionary, it will cause a crash
 */
- (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;
}
/**
 The dictionary into a string, text format utf8, and format
 @Param level the level of the current dictionary, at least 1, representing the outermost dictionary
 @Return the formatted string
 */
- (NSString *)ZY_descriptionWithLevel:(int)level {
 NSString *subSpace = [self ZY_getSpaceWithLevel:level];
 NSString *space = [self ZY_getSpaceWithLevel:level - 1];
 NSMutableString *retString = [[NSMutableString alloc] init];
 //1. Add{
 [retString appendString:[NSString stringWithFormat:@"{"]];
 //2. Add 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. Add}
 [retString appendString:[NSString stringWithFormat:@"\n%@}", space]];
 return retString;
}
/**
 Returns the preceding space placeholder according to the hierarchy
 @The hierarchy of param level dictionary
 @Return space
 */
- (NSString *)ZY_getSpaceWithLevel:(int)level {
 NSMutableString *mustr = [[NSMutableString alloc] init];
 for (int i=0; i<level; i++) {
  [mustr appendString:@"\t"];
 }
 return mustr;
}
#endif
@end

effect

这里写图片描述

And directly is the JSON string, copy the log log, directly used in the JSON formatting tool

这里写图片描述

这里写图片描述

reference resources

JLLogEncoding

summary

The above is a summary of how to solve the problem that xcode9 log can’t output Chinese in IOS. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!