美文网首页ios开发
将Xcode中控制台的Unicode转为中文

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

作者: 践行者_Leng | 来源:发表于2019-08-04 23:06 被阅读0次

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

    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
    
    

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

    相关文章

      网友评论

        本文标题:将Xcode中控制台的Unicode转为中文

        本文链接:https://www.haomeiwen.com/subject/kzuudctx.html