第一步、新建一個model類,去除@interface到@end
//
// ZAUnicode.h
// AnShunProcuratorate
//
// Created by Fedtech on 2019/12/27.
// Copyright ? 2019年 縱昂. All rights reserved.
// 簡書:http://www.reibang.com/p/c7d2fe799b77
#import <Foundation/Foundation.h>
//以下都刪除或注釋捧搞,保留#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface ZAUnicode : NSObject
@end
NS_ASSUME_NONNULL_END
第二步、在.m中寫代碼
//
// ZXPUnicode.m
//
// Created by 縱昂 on 2020/3/11.
// Copyright ? 2020年 縱昂. All rights reserved.
//
// Uncode轉(zhuǎn)中文
#import "ZAUnicode.h"
#import <objc/runtime.h>
static inline void zxp_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = 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);
}
}
@implementation NSString (ZAUnicode)
- (NSString *)stringByReplaceUnicode {
NSMutableString *convertedString = [self 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;
}
@end
@implementation NSArray (ZAUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
zxp_swizzleSelector(class, @selector(description), @selector(zxp_description));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(zxp_descriptionWithLocale:));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(zxp_descriptionWithLocale:indent:));
});
}
/**
* 我覺得
* 可以把以下的方法放到一個NSObject的category中
* 然后在需要的類中進行swizzle
* 但是又覺得這樣太粗暴了狮荔。。介粘。殖氏。
*/
- (NSString *)zxp_description {
return [[self zxp_description] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale {
return [[self zxp_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self zxp_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
@implementation NSDictionary (ZAUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
zxp_swizzleSelector(class, @selector(description), @selector(zxp_description));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(zxp_descriptionWithLocale:));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(zxp_descriptionWithLocale:indent:));
});
}
- (NSString *)zxp_description {
return [[self zxp_description] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale {
return [[self zxp_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self zxp_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
@implementation NSSet (ZAUnicode)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
zxp_swizzleSelector(class, @selector(description), @selector(zxp_description));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(zxp_descriptionWithLocale:));
zxp_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(zxp_descriptionWithLocale:indent:));
});
}
- (NSString *)zxp_description {
return [[self zxp_description] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale {
return [[self zxp_descriptionWithLocale:locale] stringByReplaceUnicode];
}
- (NSString *)zxp_descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
return [[self zxp_descriptionWithLocale:locale indent:level] stringByReplaceUnicode];
}
@end
第三步、完活了姻采。創(chuàng)建完這個類目雅采,在重啟項目會把Unicode碼轉(zhuǎn)成中文啦
圖片描述