Xcode打印中文乱码的正确输出

我们新建一个Xcode工程在控制台打印一些简单的信息,在这里以一个字典为例:

    NSDictionary *dict = @{
                           @"userName":@"刘高见",
                           @"sex":@"man",
                           @"des":@{
                                   @"message":@"我是一个iOS开发爱好者",
                                   @"title":@"同时也是你的不到的爸爸",
                                   @"reson":@"为什么这么说呢,因为我XX啊"
                                   },
                           };
    NSLog(@"%@",dict);
what's the Fuck!!!!

什么鬼,堂堂的中华大地,中文怎么就不能好好的展示。兄台莫慌张,着其实是因为编码问题导致的乱码所致的。那么,怎么解决呢?

首先,我们知道在代码打印或者执行po操作打印的时候,有几个系统方法会被调用:

-(NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level;
-(NSString *)descriptionWithLocale:(id)locale;
-(NSString *)debugDescription;

关于这几个方法何时调用,在此不在阐述:
我们可以从这几个方法着手,重写方法的实现内容,实现中文标准输出的效果。废话不多说,代码如下:

#ifdef DEBUG

#import "LGJStandardLog.h"
#import 

@implementation NSObject (LGJStandardLog)

/**
 将对象转化成json字符串
 */
-(NSString *)convertToJsonString{
    
    //判断能不能转化 不能转化返回nil
    if (![NSJSONSerialization isValidJSONObject:self]) {
        return nil;
    }
    NSError *error = nil;
    
    //NSJSONWritingOptions的值有两种情况
    //NSJSONWritingPrettyPrinted是将生成的json数据格式化输出
    //NSJSONWritingSortedKeys是将生成的json数据不尽兴格式化输出 在一行上显示
    
    NSJSONWritingOptions jsonOptions = NSJSONWritingPrettyPrinted;
    if (@available(iOS 11.0, *)) {
        //11.0之后,可以将JSON按照key排列后输出,看起来会更舒服
        jsonOptions = NSJSONWritingPrettyPrinted | NSJSONWritingSortedKeys;
    }
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    
    if (error || !jsonData) {
        return nil;
    }
    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}


@end



/**
 方法交换
 */
static inline void lgj_swizzleSelector(Class class, SEL originalSelector, SEL swizzledSelector) {
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL addMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    
    if (addMethod) {
        class_replaceMethod(class, swizzledSelector,method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    }else{
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}



@implementation NSDictionary (LGJStandardLog)

/**
 改方法是在代码控制打印的时候调用
 */
-(NSString *)standardlog_descriptionWithLocale:(id)locale{
    
    NSString *result = [self convertToJsonString];
    if (!result) {
        
        //在此有些同学会一脸懵逼 这不是在掉自己么 呵呵呵呵。。。。。。因为在load方法执行的z时候已经执行了方法的交换 此时调用standardlog_descriptionWithLocale方法相当于调用系统的descriptionWithLocale
        result = [self standardlog_descriptionWithLocale:locale];
        return result;
    }
    return  result;
}

/**
 改方法是在代码控制打印的时候调用
 */
-(NSString *)standardlog_descriptionWithLocale:(id)locale indent:(NSUInteger)level{
    
    NSString *result = [self convertToJsonString];
    if (!result) {
        result = [self standardlog_descriptionWithLocale:locale indent:level];
        return result;
    }
    return  result;
    
}

/**
 改方法是在控制太po命令打印的时候调用
 */
-(NSString *)standardlog_debugDescription{
    
    NSString *result = [self convertToJsonString];
    if (!result) {
        result = [self standardlog_debugDescription];
        return result;
    }
    return  result;
    
}


/**
 方法交换的实现
 */
+(void)load{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        lgj_swizzleSelector(class, @selector(debugDescription), @selector(standardlog_debugDescription));
        lgj_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(standardlog_descriptionWithLocale:indent:));
        lgj_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(standardlog_descriptionWithLocale:));
    });
}

@end


@implementation NSArray (LGJStandardLog)

-(NSString *)standardlog_descriptionWithLocale:(id)locale{
    
    NSString *result = [self convertToJsonString];
    if (!result) {
        result = [self standardlog_descriptionWithLocale:locale];
        return result;
    }
    return  result;
}

-(NSString *)standardlog_descriptionWithLocale:(id)locale indent:(NSUInteger)level{
    
    NSString *result = [self convertToJsonString];
    if (!result) {
        result = [self standardlog_descriptionWithLocale:locale indent:level];
        return result;
    }
    return  result;
}

-(NSString *)standardlog_debugDescription{
    
    NSString *result = [self convertToJsonString];
    if (!result) {
        result = [self standardlog_debugDescription];
        return result;
    }
    return  result;
}

/**
 方法交换的实现
 */
+(void)load{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        lgj_swizzleSelector(class, @selector(debugDescription), @selector(standardlog_debugDescription));
        lgj_swizzleSelector(class, @selector(descriptionWithLocale:indent:), @selector(standardlog_descriptionWithLocale:indent:));
        lgj_swizzleSelector(class, @selector(descriptionWithLocale:), @selector(standardlog_descriptionWithLocale:));
    });
}
@end


#endif

使用起来相当简单,直接把LGJStandardLog类扔进工程中,无需任何操作,即可解决问题。

我爱我的国

你可能感兴趣的:(Xcode打印中文乱码的正确输出)