闲言简之,代码先观为敬。
WTEscape
描述
- 解决xcode控制台打印,中文显示为Unicode编码的问题。
- 打印对象的时候,可以将对象的属性值打印出来。
使用方法
只需将
*+WTEscape
类引入您的项目中,无需进行其他任何操作和配置,在debug模式下控制台输出的Unicode即可显示为中文。打印对象的时候,对象的属性值也会一并打印。
原理简介之
1、基础之数据类型打印
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
以此法为引,文之重组之,终以字符串拼接之,遂控制台打印可解之。
2、对象属性之打印
- (NSString *)description;
唯基类入手焉得善果,NSObject以此法为引。
unsigned int ivarsCount = 0;
Ivar *ivars = class_copyIvarList([self class], &ivarsCount);
for (int i = 0; i < ivarsCount; i++) {
NSString *key = [NSString stringWithUTF8String:ivar_getName(ivars[i])];
id objct = [self valueForKey:key];
...
...
...
}
free(ivars);
运行时辅之,遍寻属性之值做字符串拼接状,而后输出可阅之果。
例之可视也
NSArray *array = @[@"你好", @"他好", @"大家好"];
NSDictionary *dataDic = @{@"姓名" : @"李四",
@"性别" : @"男",
@"爱好" : @{@"游泳" : @"不会", @"篮球" : @"也不会", @"骑车" : @"马马虎虎"},
@"朋友" : @[@"大亨", @"jobson", @"李磊"]
};
NSData *data = [NSJSONSerialization dataWithJSONObject:dataDic options:0 error:NULL];
NSSet *set = [[NSSet alloc] initWithObjects:@"这是一个set", data, dataDic, array, nil];
UIImage *image = [UIImage imageNamed:@"WTIMAGE.png"];
Car *car = [[Car alloc] init];
car.brand = @"Benze";
car.color = [UIColor redColor];
car.image = image;
car.rect = CGRectMake(0, 0, 100, 100);
NSDictionary *dictionary = @{@"姓名" : @"张三",
@"性别" : @"男",
@"data" : data,
@"set" : set,
@"array" : array,
@"image" : image,
@"car" : car,
};
NSLog(@"%@",dictionary);
如上之对象,未用WTEscape修饰之,打印如下也
2018-09-21 10:09:39.253147+0800 WTEscape[71500:3093768] {
array = (
"\U4f60\U597d",
"\U4ed6\U597d",
"\U5927\U5bb6\U597d"
);
car = "<Car: 0x600000098010>";
data = <7b22e5a7 93e5908d 223a22e6 9d8ee59b 9b222c22 e680a7e5 88ab223a 22e794b7 222c22e6 9c8be58f 8b223a5b 22e5a4a7 e4baa822 2c226a6f 62736f6e 222c22e6 9d8ee7a3 8a225d2c 22e788b1 e5a5bd22 3a7b22e7 afaee790 83223a22 e4b99fe4 b88de4bc 9a222c22 e9aa91e8 bda6223a 22e9a9ac e9a9ace8 998ee899 8e222c22 e6b8b8e6 b3b3223a 22e4b88d e4bc9a22 7d7d>;
image = "<UIImage: 0x6000000bfe60>, {378, 478}";
set = "{(\n (\n \"\\U4f60\\U597d\",\n \"\\U4ed6\\U597d\",\n \"\\U5927\\U5bb6\\U597d\"\n ),\n <7b22e5a7 93e5908d 223a22e6 9d8ee59b 9b222c22 e680a7e5 88ab223a 22e794b7 222c22e6 9c8be58f 8b223a5b 22e5a4a7 e4baa822 2c226a6f 62736f6e 222c22e6 9d8ee7a3 8a225d2c 22e788b1 e5a5bd22 3a7b22e7 afaee790 83223a22 e4b99fe4 b88de4bc 9a222c22 e9aa91e8 bda6223a 22e9a9ac e9a9ace8 998ee899 8e222c22 e6b8b8e6 b3b3223a 22e4b88d e4bc9a22 7d7d>,\n \"\\U8fd9\\U662f\\U4e00\\U4e2aset\",\n {\n \"\\U59d3\\U540d\" = \"\\U674e\\U56db\";\n \"\\U6027\\U522b\" = \"\\U7537\";\n \"\\U670b\\U53cb\" = (\n \"\\U5927\\U4ea8\",\n jobson,\n \"\\U674e\\U78ca\"\n );\n \"\\U7231\\U597d\" = {\n \"\\U6e38\\U6cf3\" = \"\\U4e0d\\U4f1a\";\n \"\\U7bee\\U7403\" = \"\\U4e5f\\U4e0d\\U4f1a\";\n \"\\U9a91\\U8f66\" = \"\\U9a6c\\U9a6c\\U864e\\U864e\";\n };\n }\n)}";
"\U59d3\U540d" = "\U5f20\U4e09";
"\U6027\U522b" = "\U7537";
}
吾辈不可识之,岂不郁郁也?非也,吾使WTEscape适之,然输出如下。
2018-09-21 10:13:17.965598+0800 WTEscape[71586:3097307]
{
姓名 = "张三",
set = (
(
"你好",
"他好",
"大家好",
),
{
姓名 = "李四",
性别 = "男",
朋友 = (
"大亨",
"jobson",
"李磊",
),
爱好 = {
篮球 = "也不会",
骑车 = "马马虎虎",
游泳 = "不会",
},
},
"这是一个set",
{
姓名 = "李四",
性别 = "男",
朋友 = (
"大亨",
"jobson",
"李磊",
),
爱好 = {
篮球 = "也不会",
骑车 = "马马虎虎",
游泳 = "不会",
},
},
),
data = {
姓名 = "李四",
性别 = "男",
朋友 = (
"大亨",
"jobson",
"李磊",
),
爱好 = {
篮球 = "也不会",
骑车 = "马马虎虎",
游泳 = "不会",
},
},
car = [类型:Car][内存地址:0x60000028f9b0][指针地址:0x7ffee7e857b8][_brand:Benze|_color:UIExtendedSRGBColorSpace 1 0 0 1|_image:<UIImage: 0x6000000b8840>, {378, 478}|_subCar:(null)|_rect:NSRect: {{0, 0}, {100, 100}}],
性别 = "男",
image = <UIImage: 0x6000000b8840>, {378, 478},
array = (
"你好",
"他好",
"大家好",
),
}
岂不美哉?美哉也!
或遇诡也,可解惑如下
DEBUG=1
系统自制之,尔若遇无效状,可检而配之。
WTEscape效之Debug,如若无效,可检而设之。