参考ZXPUnicode。数组
核心代码为:函数
1.给NSObject添加一个分类:spa
+(NSString *)stringByReplaceUnicode:(NSString *)string{code
NSMutableString *convertedString = [string mutableCopy];orm
[convertedString replaceOccurrencesOfString:@"\\U" withString:@"\\u" options:0 range:NSMakeRange(0,convertedString.length)];ip
CFStringRef transform = CFSTR(Any-Hex/Java);get
CFStringTransform( (__bridge CFMutableStringRef)convertedString),NULL,transform,YES);string
return convertedString;it
}io
2.给数组添加一个分类:
引入头文件 #import
注意这里用到了运行时里的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]]; //[selfreplaceDescription] 调用的是系统的description
}
- (NSString *) replaceDescriptionWithLocale:(nullable id)locale{
return [NSObject stringByReplaceUnicode:[self replaceDescriptionWithLocal:local]];
}
3.给字典添加一个分类,和数组分类相似,这里就不写了。