相信大家都遇到过,挺烦人的Xcode对中文的显示,有了下面的类就可以不用再看苹果的脸色去进行Unicode转换中文了。
//
// MyUnicodeChinese.h
//
// 简书 :https://www.jianshu.com/u/4c669da2ffa3
// 博客 :http://blog.sina.com.cn/u/5136501936
//
// Created by zsl on 2018/3/31.
// Copyright © 2018年 zsl. All rights reserved.
//
#import <Foundation/Foundation.h>
//
// MyUnicodeChinese.m
//
// 简书 :https://www.jianshu.com/u/4c669da2ffa3
// 博客 :http://blog.sina.com.cn/u/5136501936
//
// Created by zsl on 2018/3/31.
// Copyright © 2018年 zsl. All rights reserved.
//
#import "MyUnicodeChinese.h"
#import
staticinlinevoidmy_swizzleSelector(Classclass,SELoriginalSelector,SELswizzledSelector) {
MethodoriginalMethod =class_getInstanceMethod(class, originalSelector);
MethodswizzledMethod =class_getInstanceMethod(class, swizzledSelector);
if(class_addMethod(class, originalSelector,method_getImplementation(swizzledMethod),method_getTypeEncoding(swizzledMethod))) {
class_replaceMethod(class, swizzledSelector,method_getImplementation(originalMethod),method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
@implementationNSString (MyUnicodeChinese)
- (NSString*)stringByReplaceUnicode {
NSMutableString*convertedString = [selfmutableCopy];
[convertedStringreplaceOccurrencesOfString:@"\\U"
withString:@"\\u"
options:0
range:NSMakeRange(0, convertedString.length)];
CFStringReftransform =CFSTR("Any-Hex/Java");
CFStringTransform((__bridgeCFMutableStringRef)convertedString,NULL, transform,YES);
returnconvertedString;
}
@end
@implementationNSArray (MyUnicodeChinese)
+ (void)load {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
Classclass = [selfclass];
my_swizzleSelector(class,@selector(description),@selector(my_description));
my_swizzleSelector(class,@selector(descriptionWithLocale:),@selector(my_descriptionWithLocale:));
my_swizzleSelector(class,@selector(descriptionWithLocale:indent:),@selector(my_descriptionWithLocale:indent:));
});
}
- (NSString*)my_description {
return [[self my_description] stringByReplaceUnicode];
}
- (NSString*)my_descriptionWithLocale:(nullableid)locale {
return [[self my_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString*)my_descriptionWithLocale:(nullableid)locale indent:(NSUInteger)level {
return [[self my_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
@implementationNSDictionary (MyUnicodeChinese)
+ (void)load {
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
Classclass = [selfclass];
my_swizzleSelector(class,@selector(description),@selector(my_description));
my_swizzleSelector(class,@selector(descriptionWithLocale:),@selector(my_descriptionWithLocale:));
my_swizzleSelector(class,@selector(descriptionWithLocale:indent:),@selector(my_descriptionWithLocale:indent:));
});
}
- (NSString *)my_description {
return[[selfmy_description] stringByReplaceUnicode];
}
- (NSString *)my_descriptionWithLocale:(nullableid)locale {
return[[selfmy_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)my_descriptionWithLocale:(nullableid)locale indent:(NSUInteger)level {
return[[selfmy_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
@implementationNSSet (MyUnicodeChinese)
+ (void)load {
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [selfclass];
my_swizzleSelector(class,@selector(description),@selector(my_description));
my_swizzleSelector(class,@selector(descriptionWithLocale:),@selector(my_descriptionWithLocale:));
my_swizzleSelector(class,@selector(descriptionWithLocale:indent:),@selector(my_descriptionWithLocale:indent:));
});
}
- (NSString *)my_description {
return[[selfmy_description] stringByReplaceUnicode];
}
- (NSString *)my_descriptionWithLocale:(nullableid)locale {
return[[selfmy_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)my_descriptionWithLocale:(nullableid)locale indent:(NSUInteger)level {
return[[selfmy_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
注:导入.h和.m文件之后,直接运行就好,不用调用。
网友评论