美文网首页
iOS 控制台输出utf8转中文

iOS 控制台输出utf8转中文

作者: JohnayXiao | 来源:发表于2019-01-14 18:27 被阅读14次

    在开发中,我们调试接口时最多的就是用NSLog 或者是打断点来po 数据,然而NSLog 输出的数据中,中文是UFT-8格式显示的,根本无法调试,所以为了解决这个问题,我在网上找了一些大神的做法,也尝试了几种方法,发现了一种比较好用的,今天就分享给大家。最终效果也很好

    分别创建NSArray 和 NSDictionary的category,将代码粘贴进.m文件里面就OK

    #import <objc/runtime.h>
    
    @implementation NSDictionary (log)
    
    + (void)load
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            zx_swizzleSelector([self class], @selector(descriptionWithLocale:indent:), @selector(zx_descriptionWithLocale:indent:));
        });
    }
    - (NSString *)zx_descriptionWithLocale:(id)locale indent:(NSUInteger)level
    {
        return [self stringByReplaceUnicode:[self zx_descriptionWithLocale:locale indent:level]];
    }
    - (NSString *)stringByReplaceUnicode:(NSString *)unicodeString
    {
        NSMutableString *convertedString = [unicodeString 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;
    }
    static inline void zx_swizzleSelector(Class theClass, SEL originalSelector, SEL swizzledSelector)
    {
        Method originalMethod = class_getInstanceMethod(theClass, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(theClass, swizzledSelector);
        
        BOOL didAddMethod =
        class_addMethod(theClass,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        
        if (didAddMethod) {
            class_replaceMethod(theClass,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    }
    @end
    

    相关文章

      网友评论

          本文标题:iOS 控制台输出utf8转中文

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