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

#ifdef DEBUG
#define NSLog(FORMAT, ...) fprintf(stderr,"\n %s:%d %s\n",[[[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:@"\n%@\"%@\",", 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:@"\n%@%@,", 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:@"\n%@%@,", subSpace, str];

[retString appendString:str];

} else {

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

[retString appendString:subString];

}

}];

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

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

}

// 3、添加 ]

[retString appendString:[NSString stringWithFormat:@"\n%@]", 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:@"\t"];

}

return mustr;

}

#endif

@end

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

另外只重写的方法

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale

{

NSMutableString *str = [NSMutableString string];

[str appendString:@"{\n"];

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

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

[str appendFormat:@"\t%@ = %@,\n", 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:@"[\n"];

// 遍历数组的所有元素

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

[str appendFormat:@"%@,\n", 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 (\n", (unsigned long)self.count];

for (id obj in self) {

[str appendFormat:@"\t%@, \n", 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:@"{\t\n "];

for (NSString *key in allKeys) {

id value= self[key];

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

}

[str appendString:@"}"];

return str;

}

@end

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

iOS网络请求返回的汉字在日志里会显示成\U63d0\U4ea4\U6210\U529f,用下面的脚本就可以把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 '\U63d0\U4ea4\U6210\U529f'就可以显示汉字了。

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

Xcode中控制台中打印中文处理的更多相关文章

  1. xcode中自定义log打印

    打印内容包括 在哪个文件中 ? 在哪个方法中? 将要执行什么操作?   // 此打印实现前提: // 1.在.pch文件中实现自定义log打印方法,log名换为LCLog // 2.定义一个宏obje ...

  2. spring boot 在IDEA控制台中打印彩色日志

    只需要在application.properties中加入 spring.output.ansi.enabled=ALWAYS 即可

  3. Java控制台中输入中文输出乱码的解决办法

    Run---Run Configurations---Common---Encoding---Other---GBK Run Configurations里的Common中将编码方式改成GBK就正常了

  4. 关于 JavaScript 中一个小细节问题 (在控制台中直接 {Name:'王尼玛',Age:20} 对象报错问题)

    在 Chrome 浏览器,大家可能遇到这样一个小问题. 随便输入一个 Object 对象  ,比如 {Name:'王尼玛',Age:20} ,将会报错.之前,也从来没去考虑过到底是为啥原因. 今天,刚 ...

  5. python中打印中文

    python中打印中文 在python 2.x版本中,默认是ASCII编码方式,在有业务需要输入中文时,就会出现乱码的情况.解决这种问题的一个方式就是设置py文件的编码方式.实现方式如下: 在py文件 ...

  6. Xcode中利用git源代码版本号控制

    git是一个版本号控制系统,能够通过命令行来调用,也有专门的桌面软件.这里主要介绍在Xcode中怎样利用git来进行版本号的控制. 一.创建git源 从Xcode5開始引入了使用git的一些新特性.将 ...

  7. python在windows系统中打印中文乱码

    转自:http://www.111cn.net/phper/python/58920.htm 中文乱码对于程序开发人员来讲不是什么怪事情了,今天我在使用python打印中文时就出现乱码了,下面我们一起 ...

  8. php部分:网页中报表的打印,并用CSS样式控制打印的部分;

    网页中报表的打印,是通过调用window对象中的print()方法实现打印功能的: 调用浏览器本身的打印功能实现打印 <a href="#" onclick="wi ...

  9. 发现日志文件和打印在eclipse控制台中的编码不一致

    发现日志文件和打印在eclipse控制台中的编码不一致,正好相反. 日志文件是用notepad打开的,notepad有自己的编码方式,查询编码为utf-8,日志文件汉字等等显示正常. 但是在eclip ...

随机推荐

  1. MATLAB与C语言对比实例:随机数生成

    MATLAB与C语言对比实例:随机数生成 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.整型随机数生成函数 1.C语言程序 int intrand ...

  2. (转)Spring Boot(十二):Spring Boot 如何测试打包部署

    http://www.ityouknow.com/springboot/2017/05/09/spring-boot-deploy.html 有很多网友会时不时的问我, Spring Boot 项目如 ...

  3. 雅礼集训【Day6-1】字符串

    雅礼集训[Day6-1]字符串 假设我们有串\(a\),我们设\(a'\)为\(a\)翻转后按为取反过后的串. 我们只考虑前一半的,长为\(m\)的串.如果前半截匹配了\(a\)或者\(a'\),则\ ...

  4. Hibernate基本映射类型

  5. linux 下的启动项

    /etc/profile  这个也是启动脚本.而且优先级很高哦.. 以下都是网上找来的 (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片 ...

  6. JDK动态代理Demo代码,进一步学习分析

    import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflec ...

  7. 【vue】vue +element 搭建项目,this.$nextTick用法

    相关资料:https://www.cnblogs.com/leaf930814/p/7247478.html https://www.cnblogs.com/duanyue/p/7458340.htm ...

  8. remix的使用

    remix首先,这个东西其实是有一个线上版本的,只要登录上网址:https://remix.ethereum.org就可以直接使用了,但是我更多用的是本地配置的remix-ideremix-ide的文 ...

  9. DataGuard切换(主库为Rac+备库为Rac)

    http://blog.itpub.net/29477587/viewspace-1331121/ 前段时间做了一次主备库的切换,大体写下操作步骤和记录,分享下. 环境:           db v ...

  10. OpenCV3计算机视觉Python语言实现笔记(五)

    图像的几何变换主要包括:平移.扩大与缩小.旋转.仿射.透视等等.图像变换是建立在矩阵运算基础上的,通过矩阵运算可以很快的找到对应关系. 1. 图像的平移 图像的平移,沿着x方向tx距离,y方向ty距离 ...