当我们正常编写代码的时候,用log日志打印字典,会出现这样的结果

当我们使用下面的代码他就会变成这个样子

 

具体代码为

 1 //
 2 //  NSObject+Log.m
 3 //  字典的可读性
 4 //
 5 //  Created by ma c on 16/5/20.
 6 //  Copyright © 2016年 彭盛凇. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @implementation NSDictionary (Log)
12 - (NSString *)descriptionWithLocale:(id)locale
13 {
14     NSMutableString *string = [NSMutableString string];
15     
16     // 开头有个{
17     [string appendString:@"{\n"];
18     
19     // 遍历所有的键值对
20     [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
21         [string appendFormat:@"\t%@", key];
22         [string appendString:@" : "];
23         [string appendFormat:@"%@,\n", obj];
24     }];
25     
26     // 结尾有个}
27     [string appendString:@"}"];
28     
29     // 查找最后一个逗号
30     NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
31     if (range.location != NSNotFound)
32         [string deleteCharactersInRange:range];
33     
34     return string;
35 }
36 @end
37 
38 @implementation NSArray (Log)
39 
40 - (NSString *)descriptionWithLocale:(id)locale
41 {
42     NSMutableString *string = [NSMutableString string];
43     
44     // 开头有个[
45     [string appendString:@"[\n"];
46     
47     // 遍历所有的元素
48     [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
49         [string appendFormat:@"\t%@,\n", obj];
50     }];
51     
52     // 结尾有个]
53     [string appendString:@"]"];
54     
55     // 查找最后一个逗号
56     NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
57     if (range.location != NSNotFound)
58         [string deleteCharactersInRange:range];
59     
60     return string;
61 }
62 
63 @end
NSObject(Log)