/*
新建一个分类,打印带中文的NSDictionary和NSArray就不会再出现乱码了:
*/
#import <Foundation/Foundation.h>
@implementation NSDictionary (Log)
-(NSString *)descriptionWithLocale:(id)locale
{
NSArray *allKeys = [self allKeys];
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{\n"];
for (NSString *key in allKeys) {
id value= self[key];
[str appendFormat:@"\t%@ = %@;\n",key, value];
}
[str appendString:@"}"];
return str;
}
@end
@implementation NSArray (Log)
-(NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *str = [NSMutableString string];
//拼接
[str appendString:@"("];
[self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[str appendString:[NSString stringWithFormat:@"\n\t%@,",obj]];
}];
[str appendString:@"\n)"];
//NSBackwardsSearch从后往前搜索
NSRange rang = [str rangeOfString:@"," options:NSBackwardsSearch];
if (rang.location != NSNotFound) {
[str deleteCharactersInRange:rang];
}
return str;
}
@end