将Xcode中控制台的Unicode转为中文

废话不多说,直接看截图和上代码 (有问题可以留言)。

image.png

image.png

image.png

image.png

代码如下

在NSObject分类中的.h文件中的代码

//
//  NSObject+ChangeUnicode.h
//  yoli
//
//  Created by 冷求慧 on 17/3/14.
//  Copyright © 2017年 Leng. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSObject (ChangeUnicode)

+(NSString *)stringByReplaceUnicode:(NSString *)string;

@end

@interface NSArray (LengUnicode)

@end

@interface NSDictionary (LengUnicode)

@end

在NSObject分类中的.m文件中的代码

//
//  NSObject+ChangeUnicode.m
//  yoli
//
//  Created by 冷求慧 on 17/3/14.
//  Copyright © 2017年 Leng. All rights reserved.
//

#import "NSObject+ChangeUnicode.h"

#import <objc/runtime.h>   // 导入运行时

@implementation NSObject (ChangeUnicode)

+(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;
}
@end

@implementation NSArray (LengUnicode)

+ (void)load {
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(description)), class_getInstanceMethod([self class], @selector(replaceDescription)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:indent:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:indent:)));
}

- (NSString *)replaceDescription {
    return [NSObject stringByReplaceUnicode:[self replaceDescription]];
}

- (NSString *)replaceDescriptionWithLocale:(nullable id)locale {
    return [NSObject stringByReplaceUnicode:[self replaceDescriptionWithLocale:locale]];
}

- (NSString *)replaceDescriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
    return [NSObject stringByReplaceUnicode:[self replaceDescriptionWithLocale:locale indent:level]];
}
@end

@implementation NSDictionary (LengUnicode)

+ (void)load {
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(description)), class_getInstanceMethod([self class], @selector(replaceDescription)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:)));
    method_exchangeImplementations(class_getInstanceMethod([self class], @selector(descriptionWithLocale:indent:)), class_getInstanceMethod([self class], @selector(replaceDescriptionWithLocale:indent:)));
}

- (NSString *)replaceDescription {
    return [NSObject stringByReplaceUnicode:[self replaceDescription]];
}

- (NSString *)replaceDescriptionWithLocale:(nullable id)locale {
    return [NSObject stringByReplaceUnicode:[self replaceDescriptionWithLocale:locale]];
}

- (NSString *)replaceDescriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {
    return [NSObject stringByReplaceUnicode:[self replaceDescriptionWithLocale:locale indent:level]];
}
@end

最后直接再重新运行一下项目,看下控制台的打印即可!!!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 点我进入最新的修改地址(使用了Markdown),可以直接复制。
    践行者_Leng阅读 1,501评论 1 3
  • 三四节本该上课,因为接待参观调了课。忽然觉得很空,没有学生和热闹的课堂,反而觉得不习惯。
    爱谁谁7777阅读 258评论 0 0
  • 我们的肠胃由不得我们把控。很多人会吃得好好的,然后莫名其妙就拉肚子,不舒服。甚至,感觉吃东西都很讲究,为什么...
    jackhot阅读 145评论 0 0