• Xcode中控制台中打印中文处理


    xcode 10以后的方法,一般使用

    #ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"
     %s:%d   %s
    ",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String],__LINE__, [[[NSString alloc] initWithData:[[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] dataUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding] UTF8String]);
    #else
    #define NSLog(...)
    #endif

    xcode 10以前的是方法一般重写NSArray的descriptionWithLocale  方法

    //

    //  NSDictionary+CBLog.m

    //  CaiBao

    //

    //  Created by cb_2018 on 2019/2/21.

    //  Copyright © 2019 91cb. All rights reserved.

    //

    #import "NSDictionary+CBLog.h"

    #import <objc/runtime.h>

    @implementation NSDictionary (CBLog)

    #ifdef DEBUG

    + (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);

        }

    }

    #endif

    @end

     

    //

    //  NSArray+CBLog.m

    //  CaiBao

    //

    //  Created by cb_2018 on 2019/2/21.

    //  Copyright © 2019 91cb. All rights reserved.

    //

    #import "NSArray+CBLog.h"

    @implementation NSArray (CBLog)

    #ifdef DEBUG

    - (NSString *)description {

        return [self jl_descriptionWithLevel:1];

    }

    -(NSString *)descriptionWithLocale:(id)locale

    {

        return [self jl_descriptionWithLevel:1];

    }

    - (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level {

        return [self jl_descriptionWithLevel:(int)level];

    }

    /**

     将数组转化成字符串,文字格式UTF8,并且格式化

     @param level 当前数组的层级,最少为 1,代表最外层

     @return 格式化的字符串

     */

    - (NSString *)jl_descriptionWithLevel:(int)level {

        NSString *subSpace = [self jl_getSpaceWithLevel:level];

        NSString *space = [self jl_getSpaceWithLevel:level - 1];

        NSMutableString *retString = [[NSMutableString alloc] init];

        // 1、添加 [

        [retString appendString:[NSString stringWithFormat:@"["]];

        // 2、添加 value

        [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            if ([obj isKindOfClass:[NSString class]]) {

                NSString *value = (NSString *)obj;

                value = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

                NSString *subString = [NSString stringWithFormat:@" %@"%@",", subSpace, value];

                [retString appendString:subString];

            } else if ([obj isKindOfClass:[NSArray class]]) {

                NSArray *arr = (NSArray *)obj;

                NSString *str = [arr jl_descriptionWithLevel:level + 1];

                str = [NSString stringWithFormat:@" %@%@,", subSpace, str];

                [retString appendString:str];

            } else if ([obj isKindOfClass:[NSDictionary class]]) {

                NSDictionary *dic = (NSDictionary *)obj;

                NSString *str = [dic descriptionWithLocale:nil indent:level + 1];

                str = [NSString stringWithFormat:@" %@%@,", subSpace, str];

                [retString appendString:str];

            } else {

                NSString *subString = [NSString stringWithFormat:@" %@%@,", subSpace, obj];

                [retString appendString:subString];

            }

        }];

        if ([retString hasSuffix:@","]) {

            [retString deleteCharactersInRange:NSMakeRange(retString.length-1, 1)];

        }

        // 3、添加 ]

        [retString appendString:[NSString stringWithFormat:@" %@]", space]];

        return retString;

    }

    /**

     根据层级,返回前面的空格占位符

     @param level 层级

     @return 占位空格

     */

    - (NSString *)jl_getSpaceWithLevel:(int)level {

        NSMutableString *mustr = [[NSMutableString alloc] init];

        for (int i=0; i<level; i++) {

            [mustr appendString:@" "];

        }

        return mustr;

    }

    #endif

    @end

     、、、、、、、、、、、、、、、、、

    另外只重写的方法

    @implementation NSDictionary (Log)

    - (NSString *)descriptionWithLocale:(id)locale

    {

        NSMutableString *str = [NSMutableString string];

        

        [str appendString:@"{ "];

        

        // 遍历字典的所有键值对

        [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {

            [str appendFormat:@" %@ = %@, ", key, obj];

        }];

        

        [str appendString:@"}"];

        

        // 查出最后一个,的范围

        NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];

        if (range.length != 0) {

            // 删掉最后一个,

            [str deleteCharactersInRange:range];

        }

        

        return str;

    }

    @end

    @implementation NSArray (Log)

    - (NSString *)descriptionWithLocale:(id)locale

    {

        NSMutableString *str = [NSMutableString string];

        

        [str appendString:@"[ "];

        

        // 遍历数组的所有元素

        [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

            [str appendFormat:@"%@, ", obj];

        }];

        

        [str appendString:@"]"];

        

        // 查出最后一个,的范围

        NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];

        if (range.length != 0) {

            // 删掉最后一个,

            [str deleteCharactersInRange:range];

        }

        

        return str;

    }

    @end

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    iOS Xcode打印汉字, 而不是UTF8编码

    2017年06月21日 15:15:41 小朱泽龙 阅读数:237

    http://blog.csdn.net/Cloud_Pro/article/details/53391656

    为NSArray添加分类

    #import "NSArray+decription.h"  

      

    @implementation NSArray (decription)  

      

    - (NSString *)descriptionWithLocale:(id)locale  

    {  

        NSMutableString *str = [NSMutableString stringWithFormat:@"%lu ( ", (unsigned long)self.count];  

          

        for (id obj in self) {  

            [str appendFormat:@" %@,  ", obj];  

        }  

          

        [str appendString:@")"];  

          

        return str;  

    }  

      

    @end

    为NSDictionary添加分类

    #import "NSDictionary+decription.h"  

      

    @implementation NSDictionary (decription)  

      

    - (NSString *)descriptionWithLocale:(id)locale  

    {  

        NSArray *allKeys = [self allKeys];  

        NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"{  "];  

        for (NSString *key in allKeys) {  

            id value= self[key];  

            [str appendFormat:@"  "%@" = %@, ",key, value];  

        }  

        [str appendString:@"}"];  

          

        return str;  

    }  

      

    @end

    、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    iOS网络请求返回的汉字在日志里会显示成U63d0U4ea4U6210U529f,用下面的脚本就可以把Unicode显示成汉字

    #!/bin/sh

    cmd="/usr/bin/env python -c 'print("""$1""".lower().decode("unicode-escape").encode("utf-8"))'"

    echo $cmd | sh

    比如:把这个脚本命名为co,并使用chmod +x co添加可执行属性

    使用co 'U63d0U4ea4U6210U529f'就可以显示汉字了。

    链接:https://www.jianshu.com/p/

  • 相关阅读:
    4.内核编译和裁剪
    2.Linux技能要求
    3.字符驱动框架
    1.Linux命令
    4.类和抽象
    3.指针
    2.C++语言特性
    1.编译器
    计数排序——Counting Sort
    网关、网桥、路由器、集线器
  • 原文地址:https://www.cnblogs.com/sundaysgarden/p/10412333.html
Copyright © 2020-2023  润新知