参考ZXPUnicode。
核心代码为:
1.给NSObject添加一个分类:
+(NSString *)stringByReplaceUnicode:(NSString *)string{
NSMutableString *convertedString = [string mutableCopy];
[convertedString replaceOccurrencesOfString:@"\\U" withString:@"\\u" options:0 range:NSMakeRange(0,convertedString.length)];
CFStringRef transform = CFSTR(Any-Hex/Java);
CFStringTransform( (__bridge CFMutableStringRef)convertedString),NULL,transform,YES);
return convertedString;
}
2.给数组添加一个分类:
引入头文件 #import <objc/runtime.h>
注意这里用到了运行时里的method_exchangeImplementations()方法
补充说明:load函数是只要你动态加载或者静态引用了这个类,那么load就会被执行,它并不需要你显示的去创建一 个类后才会执行,同时只执行一次。 关于load的执行顺序问题,所有的superclass的load执行完以后才会执行该类的load。并且class中的load方法是先于category中的load执行的。
+(void)load{
method_exchangeImplementations(class_getInstanceMethod([self class],@selector(description)), class_getInstanceMethod([self class],@selector(replaceDescription)));//交换系统的description添加新实现
method_exchangeImplementations(class_getInstanceMethod([self class],@selector(descriptionWithLocale:)), class_getInstanceMethod([self class],@selector(replaceDescriptionWithLocale:)));
}
- (NSString *)replaceDescription{
return [NSObject stringByReplaceUnicode :[self replaceDescription]]; // [self replaceDescription] 调用的是系统的description
}
- (NSString *) replaceDescriptionWithLocale:(nullable id)locale{
return [NSObject stringByReplaceUnicode:[self replaceDescriptionWithLocal:local]];
}