When we request the interface, the server will return some data. When we print it, we will find that the printed code is unicode, not Chinese characters.
At this time, we need to deal with it manually, so that the format of Chinese characters will be output when printing.
The method is as follows:
Add a new category, and in the category, rewrite the description method as follows:
/**
* Log collection type
*/
@implementation NSArray (log)
- (NSString *)description{
return [self descriptionWithLocale:nil];
}
- (NSString *)descriptionWithLocale:(id)locale{
NSMutableString * string = [[NSMutableString alloc]init];
[string appendString:@"[ "];
for (int i = 0; i) {
[string appendFormat:@" The %dth-- %@ ",i,self[i] ];
}
[string stringByAppendingString:@"] "];
return string;
}
@end
@implementation NSDictionary (Log)
- (NSString *)jsonDescription {
// Referred to this bloghttps://www.jianshu.com/p/f14b4cb1435b.
// NSString uses UTF-16 by default, which is converted to UTF -8 can be printed
NSError * error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSMutableString *strM = [NSMutableString stringWithString:@"{ < /span>"];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[strM appendFormat:@" %@ = %@; ", key, obj];
}];
[strM appendString:@"} < span style="color: #800000;">"];
return strM;
}
NSString *newString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
newString = [newString stringByReplacingOccurrencesOfString:@"\ " withString:@"" ];
return newString;
}
- (NSString *)description{
return [self descriptionWithLocale:nil];
}
- (NSString *)descriptionWithLocale:(id)locale
{
if ([NSJSONSerialization isValidJSONObject:self]) {
return [self jsonDescription];
}
// The original way of writing, the format is a bit problematic, but it is transferred to Chinese No problem
NSMutableString *strM = [NSMutableString stringWithString:@"{ < span style="color: #800000;">"];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[strM appendFormat:@" %@ = %@; ", key, obj];
}];
[strM appendString:@"} < span style="color: #800000;">"];
return strM;
}
@end
/**
* Log collection type
*/
@implementation NSArray (log)
- (NSString *)description{
return [self descriptionWithLocale:nil];
}
- (NSString *)descriptionWithLocale:(id)locale{
NSMutableString * string = [[NSMutableString alloc]init];
[string appendString:@"[ "];
for (int i = 0; i) {
[string appendFormat:@" The %dth-- %@ ",i,self[i] ];
}
[string stringByAppendingString:@"] "];
return string;
}
@end
@implementation NSDictionary (Log)
- (NSString *)jsonDescription {
// Referred to this bloghttps://www.jianshu.com/p/f14b4cb1435b.
// NSString uses UTF-16 by default, which is converted to UTF -8 can be printed
NSError * error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSMutableString *strM = [NSMutableString stringWithString:@"{ < /span>"];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[strM appendFormat:@" %@ = %@; ", key, obj];
}];
[strM appendString:@"} < span style="color: #800000;">"];
return strM;
}
NSString *newString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
newString = [newString stringByReplacingOccurrencesOfString:@"\ " withString:@"" ];
return newString;
}
- (NSString *)description{
return [self descriptionWithLocale:nil];
}
- (NSString *)descriptionWithLocale:(id)locale
{
if ([NSJSONSerialization isValidJSONObject:self]) {
return [self jsonDescription];
}
// The original way of writing, the format is a bit problematic, but it is transferred to Chinese No problem
NSMutableString *strM = [NSMutableString stringWithString:@"{ < span style="color: #800000;">"];
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[strM appendFormat:@" %@ = %@; ", key, obj];
}];
[strM appendString:@"} < span style="color: #800000;">"];
return strM;
}
@end