Xcode控制台Unicode转中文打印

代码来自 -> @荏苒少年


// NSObject+Unicode.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface NSObject (Unicode)
+(NSString*)stringByReplaceUnicode:(NSString*)string;
@end

@interface NSArray (Unicode)

@end

@interface NSDictionary (Unicode)

@end


NS_ASSUME_NONNULL_END
// NSObject+Unicode.m

#import "NSObject+Unicode.h"
#import  

@implementation NSObject (Unicode)
+(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 (Unicode)
+ (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 (Unicode)
+ (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转中文打印)