iOS開發(fā)-Unicode轉(zhuǎn)中文

第一步、新建一個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)成中文啦

如有遇到其他方法或者問題,會持續(xù)篇發(fā)布更新
圖片描述
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末慨亲,一起剝皮案震驚了整個濱河市婚瓜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌巴刻,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胡陪,死亡現(xiàn)場離奇詭異,居然都是意外死亡碍舍,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門片橡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事吹泡÷济海” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵荞胡,是天一觀的道長妈踊。 經(jīng)常有香客問我,道長泪漂,這世上最難降的妖魔是什么廊营? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮萝勤,結(jié)果婚禮上露筒,老公的妹妹穿的比我還像新娘。我一直安慰自己敌卓,他們只是感情好慎式,可當我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著趟径,像睡著了一般瘪吏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蜗巧,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天掌眠,我揣著相機與錄音,去河邊找鬼幕屹。 笑死蓝丙,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的望拖。 我是一名探鬼主播渺尘,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼说敏!你這毒婦竟也來了鸥跟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤像云,失蹤者是張志新(化名)和其女友劉穎锌雀,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迅诬,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡腋逆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了侈贷。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惩歉。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡等脂,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出撑蚌,到底是詐尸還是另有隱情上遥,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布争涌,位于F島的核電站粉楚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏亮垫。R本人自食惡果不足惜模软,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饮潦。 院中可真熱鬧,春花似錦继蜡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春枚抵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背汽摹。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工逼泣, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人拉庶。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓氏仗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子币励,可洞房花燭夜當晚...
    茶點故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 前言 本文是研發(fā)一個在線超市的電商類APP過程中食呻,對架構(gòu)的整理澎现。 功能: 1、瀏覽商品昔头、購買商品、切換商店莱革; 2讹开、...
    落影l(fā)oyinglin閱讀 10,129評論 9 69
  • yummy0632閱讀 309評論 0 0
  • 多新鮮吶成艘,“老婆,我怕你”這句話居然出自我家老公的嘴巴断箫。要知道,我老公在家里一向剛愎自用仲义,自稱大王剑勾,啥時肯折過自己...
    笑春風zyl閱讀 759評論 2 7
  • 火車疾馳過一條條軌道,一片片麥田 田間池塘里的魚也在歡快的游 就如我這顆急切與你相見的心芝发,然后 就可以滿含深情的對...
    山朝有雨閱讀 438評論 0 0
  • 今天突然想到:我的生命力是不是在高考錄取結(jié)果公布后就消殆了?二十幾歲的人生格郁,過的懶散而不思進取独悴。 說實話,這樣很累...
    囈爾似憶閱讀 142評論 0 0