IOS打印字典乱码问题

2020-06-13  本文已影响0人  游戏创作者

很多时候我们接收到服务器传过来的的提示信息都会显示乱码:


gibberish.png

这其实是因为编码问题导致的乱码所致的,怎么解决呢?

这里我写了个分类 LGJStandardLog
直接导入工程中,无需任何操作,即可解决问题

简书不能上传附件,我把代码贴出来吧:
LGJStandardLog.h

//
//  LGJStandardLog.h
//  beijing
//
//  Created by 飞杨 on 6/13/20.
//  Copyright © 2020 zhou last. All rights reserved.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface LGJStandardLog : NSObject

@end

NS_ASSUME_NONNULL_END

LGJStandardLog.m

//
//  LGJStandardLog.m
//  beijing
//
//  Created by 飞杨 on 6/13/20.
//  Copyright © 2020 zhou last. All rights reserved.
//

#ifdef DEBUG

#import "LGJStandardLog.h"
#import <objc/runtime.h>

@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

拖进去再次运行项目,结果如下:


perfect.png
上一篇 下一篇

猜你喜欢

热点阅读