版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18683985/article/details/84990932
以前使用NSLog打印中文就是重写下面的方法.
- (NSString *)descriptionWithLocale:(nullable id)locale;
然而在某个版本(我也记不清楚是什么版本了).这个方法失效了.NSLog不会走这个方法.当时简单的查阅了一下资料就改了…今天看了看一个特别老旧的项目没有输出中文.想到之前改的这个事情了.纯属记录一下吧.
在新版本中使用
- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
来代替之前的方法.
注:下面贴出具体代码
@implementation NSArray (Log)
/// old
- (NSString *)descriptionWithLocale:(id)locale {
NSMutableString *strM = [NSMutableString string];
[strM appendString:@"(\n"];
[self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 判断是否是最后一个元素
if (idx == self.count - 1) {
[strM appendFormat:@"\t%@\n", obj];
} else {
[strM appendFormat:@"\t%@,\n", obj];
}
}];
[strM appendString:@")\n"];
return strM;
}
/// new
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
NSMutableString *strM = [NSMutableString string];
[strM appendString:@"(\n"];
[self enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// 判断是否是最后一个元素
if (idx == self.count - 1) {
[strM appendFormat:@"\t%@\n", obj];
} else {
[strM appendFormat:@"\t%@,\n", obj];
}
}];
[strM appendString:@")\n"];
return strM;
}
@end
@implementation NSDictionary (Log)
/// old
- (NSString *)descriptionWithLocale:(id)locale {
NSMutableString *strM = [NSMutableString string];
[strM appendString:@"{\n"];
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[strM appendFormat:@"\t%@ = %@;\n", key, obj];
}];
[strM appendString:@"}\n"];
return strM;
}
/// new
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level {
NSMutableString *strM = [NSMutableString string];
[strM appendString:@"{\n"];
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
[strM appendFormat:@"\t%@ = %@;\n", key, obj];
}];
[strM appendString:@"}\n"];
return strM;
}
@end