Xcode控制台中文问题

在开发中我们经常需要在控制台中打印出一些数据,以验证我们代码的正确性。一般我们的需求都是会打印出网络请求的返回结果,返回的大部分都是json数据,但是直接输出json数据时中文总会以原始码文显示,而不是正常显示中文。

head =  {
            "is_auth" = "1.0";
            "last_pack" = "1.0";
            message = "\U64cd\U4f5c\U6210\U529f";
         }

打印出的都是unicode编码,非常不方便我们迅速的理解。此时其实打印的结果基本没什么意义了。我们需要的是这样

"head" : {
      "is_auth" : "1.0",
      "last_pack" : "1.0",
      "message" : "操作成功",
      }
解决办法
  • 使用代码
    直接将json数据或者字典转换为NSData
// json数据或者NSDictionary转为NSData,responseObject为json数据或者NSDictionary
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
// NSData转为NSString
NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"jsonStr = %@", jsonStr);
  • 重写description方法,也就是为字典或者数组添加一个分类
    当字典或者数组被打印的时候,系统自动调用重写的description方法不需要将该分类导入到任何类中。
    description方法有3个方法
    • descriptionWithLocale:indent:
    • descriptionWithLocale:
    • description
      这3个方法的调用顺序如下,
      descriptionWithLocale:indent:-> descriptionWithLocale:
      -> description
      官方文档中也说明了调用顺序

The returned NSString object contains the string representations of each of the dictionary’s entries. descriptionWithLocale:indent: obtains the string representation of a given key or value as follows:
If the object is an NSString object, it is used as is.
If the object responds to descriptionWithLocale:indent:, that method is invoked to obtain the object’s string representation.
If the object responds to descriptionWithLocale:, that method is invoked to obtain the object’s string representation.
If none of the above conditions is met, the object’s string representation is obtained by through its description property.

分类代码如下

#import "NSDictionary+Log.h"

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale{
    
    NSMutableString *strM = [NSMutableString stringWithString:@"{\n"];
    
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [strM appendFormat:@"\t%@ = %@;\n", key, obj];
    }];
    
    [strM appendString:@"}\n"];
    
    return strM;
}

@end

@implementation NSArray (Log)

- (NSString *)descriptionWithLocale:(id)locale{
    NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
    
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [strM appendFormat:@"\t%@,\n", obj];
    }];
    
    [strM appendString:@")"];
    
    return strM;
}

@end
  • 使用第三方插件(不推荐)
    FKConsole是一个用于在Xcode控制台显示中文的插件。地址直达这里
    详情请查看官方的文档,这里只贴出效果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,677评论 19 139
  • 村子里总是有些狗,虽都不一样但谁也不认识是谁家的狗,所谓“打狗看主人”得到谁家的门口方知道看哪位主人。 狗大多是担...
    一缕阳光yg阅读 602评论 0 5
  • 人生下来的时候,是一团火光。然后慢慢熄灭。 ——Mr.Nav 序言 月亮硕大无比,照出周围层层的云,月亮悬在大昭寺...
    NAV辞典阅读 616评论 0 3
  • 记得是假期刚开学 晚上的火车 回学校时差点就要关门 你去车站接我 你拉着行李 我拉着你的手 风在吹,叶在摇 突然的...
    玲子lee阅读 272评论 0 0

友情链接更多精彩内容