OC 解决NSArray、NSDictionary直接打印中文出现乱码的问题

在iOS开发中,常常须要查看数组中得元素是不是本身想要的,可是苹果并无对直接打印数组中得中文做处理,直接打印就会出现一堆很讨厌的东西,解决其实很简单,就是须要经过为NSArray添加分类,重写 - (NSString *)descriptionWithLocale:(id)locale方法便可html

代码以下:数组

#import "NSArray+Log.h"

@implementation NSArray (Log)


- (NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *str = [NSMutableString stringWithFormat:@"%lu (\n", (unsigned long)self.count];
    
    for (id obj in self) {
        [str appendFormat:@"\t%@, \n", obj];
    }
    
    [str appendString:@")"];
    
    return str;
}
@end

同理要解决NSDictionary乱码问题,也须要为NSDictionary类添加一个分类,重写  - (NSString *)descriptionWithLocale:(id)locale方法app

代码以下:post

 1 #import "NSDictionary+MyLog.h"
 2 
 3 @implementation NSDictionary (MyLog)
 4 
 5 
 6 - (NSString *)descriptionWithLocale:(id)locale
 7 {
 8     NSArray *allKeys = [self allKeys];
 9     NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{\t\n "];
10     for (NSString *key in allKeys) {
11         id value= self[key];
12         [str appendFormat:@"\t \"%@\" = %@,\n",key, value];
13     }
14     [str appendString:@"}"];
15     
16     return str;
17 }
18 @end

 

转载于:https://www.cnblogs.com/pretty-guy/p/4054924.htmlspa