最近在调试的时候发现 Xcode
直接打印 NSArray
数组和 NSDictionary
字典的时候,如果其中有中文,打印出来就变成乱码了,调试起来很不舒服,下面就分享一下解决办法。
解决方法
其实很简单,只要为 NSArray
数组和 NSDictionary
字典各创建一个分类即可,并且只需要创建,无需引用或者调用。
1. NSArray 数组
数组打印结果对比
NSArray *array = @[@"张三", @"李四", @"王五"]; NSLog(@"%@", array);
|
2017-07-05 11:38:30.058 Test[8402:224709] ( "\U5f20\U4e09", "\U674e\U56db", "\U738b\U4e94" )
|
2017-07-05 11:44:23.009 test[8528:231831] ( 张三, 李四, 王五 )
|
数组分类创建内容
NSArray+Log.h
#import <Foundation/Foundation.h>
@interface NSArray (Log)
@end
|
NSArray+Log.m
#import "NSArray+Log.h"
@implementation NSArray (Log)
- (NSString *)descriptionWithLocale:(id)locale { NSMutableString *string = [NSMutableString stringWithString:@"(\n"]; [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { [string appendFormat:@"\t%@,\n", obj]; }]; if ([string hasSuffix:@",\n"]) { [string deleteCharactersInRange:NSMakeRange(string.length - 2, 1)]; } [string appendString:@")\n"]; return string; }
@end
|
2. NSDictionary 字典
字典打印结果对比
NSDictionary *dictionary = @{@"姓名1" : @"张三", @"姓名2" : @"李四", @"姓名3" : @"王五"}; NSLog(@"%@", dictionary);
|
2017-07-05 11:38:30.059 test[8402:224709] { "\U59d3\U540d1" = "\U5f20\U4e09"; "\U59d3\U540d2" = "\U674e\U56db"; "\U59d3\U540d3" = "\U738b\U4e94"; }
|
2017-07-05 11:44:23.009 test[8528:231831] { 姓名1 = 张三; 姓名2 = 李四; 姓名3 = 王五; }
|
字典分类创建内容
NSDictionary+Log.h
#import <Foundation/Foundation.h>
@interface NSDictionary (Log)
@end
|
NSDictionary+Log.m
#import "NSDictionary+Log.h"
@implementation NSDictionary (Log)
- (NSString *)descriptionWithLocale:(id)locale { NSMutableString *string = [NSMutableString stringWithString:@"{\n"]; [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [string appendFormat:@"\t%@ = %@;\n", key, obj]; }]; [string appendString:@"}\n"]; return string; }
@end
|