iOS 数组和字典NSLog及po输出中文

前言

当你搜索“iOS 数组和字典NSLog输出中文” 时
会找到:
这个 iOS开发——输出中文(字典和数组)
这个 iOS 打印中文字典,数组,控制台输出中文,并保持缩进格式
这个 解决NSLog字典,数组,集合时中文显示Unicode问题

这些内容,都是走的同一条路
1.重写分类:- (NSString *)descriptionWithLocale:(id)locale
2.遍历所有的键值对 以及 遍历所有的元素
也有 使用 NSPropertyListSerialization


正文

其实,还有个更简单的方法:
不使用遍历
重写 description (po)
重写 descriptionWithLocale: (NSLog)
就 OK 了!

#import "NSArray+JHUnicode.h"
#import <objc/runtime.h>

@implementation NSArray (JHUnicode)

+ (void)load{
    Method old = class_getInstanceMethod(self, @selector(description));
    Method new = class_getInstanceMethod(self, @selector(jh_description));
    method_exchangeImplementations(old, new);

    {
        Method old = class_getInstanceMethod(self, @selector(descriptionWithLocale:));
        Method new = class_getInstanceMethod(self, @selector(jh_descriptionWithLocale:));
        method_exchangeImplementations(old, new);
    }
}

- (NSString *)jh_description{
    NSString *description = [self jh_description];
    description = [NSString stringWithCString:[description cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];
    return description;
}

- (NSString *)jh_descriptionWithLocale:(id)local{
    return [self description];
}

@end

参考

关于ios中的转码问题

猜你喜欢

转载自blog.csdn.net/xjh093/article/details/80518873
今日推荐