开源HYBUnicodeReadable日志显示Unicode中文
                 jopen
                 10年前
            
                    前言
开发中经常需要打印日志以查看数据是否正确,或者说查看数据的格式。但是,苹果对于我们的 NSDictionary 、 NSSet 、 NSArray 等值有中文时,打印出来的是 Unicode 编码,人类无法直接读懂,因此,笔者研究研究如何将打印出来的日志保持原有的格式化且能够将 Unicode 编码打印出来是正常人类可读懂的中文。
实现原理
苹果给我们提供了本地化的方法,对于 NSDictionary 、 NSSet 、 NSArray 都可以重写该方法来实现:
NSSet实现
对于 NSSet 实现如下:
   - (NSString *)descriptionWithLocale:(id)localeindent:(NSUInteger)level {    NSMutableString *desc = [NSMutableString string];        NSMutableString *tabString = [[NSMutableString alloc]initWithCapacity:level];    for (NSUInteger i = 0; i < level; ++i) {      [tabStringappendString:@"\t"];    }        NSString *tab = @"\t";    if (level > 0) {      tab = tabString;    }    [descappendString:@"\t{(\n"];        for (id objin self) {      if ([objisKindOfClass:[NSDictionary class]]          || [objisKindOfClass:[NSArray class]]          || [objisKindOfClass:[NSSet class]]) {        NSString *str = [((NSDictionary *)obj)descriptionWithLocale:localeindent:level + 1];        [descappendFormat:@"%@\t%@,\n", tab, str];      } else if ([objisKindOfClass:[NSString class]]) {        [descappendFormat:@"%@\t\"%@\",\n", tab, obj];      } else {        [descappendFormat:@"%@\t%@,\n", tab, obj];      }    }        [descappendFormat:@"%@)}", tab];        return desc;  }   我们这里根本缩进级别来生成对应的格式化字符串,以便解决多级嵌套时格式不太美观的问题。
NSArray实现
对于 NSArray 的实现如下:
   - (NSString *)descriptionWithLocale:(id)localeindent:(NSUInteger)level {    NSMutableString *desc = [NSMutableString string];        NSMutableString *tabString = [[NSMutableString alloc]initWithCapacity:level];    for (NSUInteger i = 0; i < level; ++i) {      [tabStringappendString:@"\t"];    }        NSString *tab = @"";    if (level > 0) {      tab = tabString;    }    [descappendString:@"\t(\n"];        for (id objin self) {      if ([objisKindOfClass:[NSDictionary class]]          || [objisKindOfClass:[NSArray class]]          || [objisKindOfClass:[NSSet class]]) {        NSString *str = [((NSDictionary *)obj)descriptionWithLocale:localeindent:level + 1];        [descappendFormat:@"%@\t%@,\n", tab, str];      } else if ([objisKindOfClass:[NSString class]]) {        [descappendFormat:@"%@\t\"%@\",\n", tab, obj];      } else {        [descappendFormat:@"%@\t%@,\n", tab, obj];      }    }        [descappendFormat:@"%@)", tab];        return desc;  }   同样的道理,不过笔者对于字符串值,都加上了双引号,一眼就可以看出来是字符串类型。
NSDictionary实现
对于NSDictonary的实现如下:
   - (NSString *)descriptionWithLocale:(id)localeindent:(NSUInteger)level {    NSMutableString *desc = [NSMutableString string];        NSMutableString *tabString = [[NSMutableString alloc]initWithCapacity:level];    for (NSUInteger i = 0; i < level; ++i) {      [tabStringappendString:@"\t"];    }        NSString *tab = @"";    if (level > 0) {      tab = tabString;    }        [descappendString:@"\t{\n"];        // 遍历数组,self就是当前的数组    for (id keyin self.allKeys) {      id obj = [selfobjectForKey:key];            if ([objisKindOfClass:[NSString class]]) {        [descappendFormat:@"%@\t%@ = \"%@\",\n", tab, key, obj];      } else if ([objisKindOfClass:[NSArray class]]                || [objisKindOfClass:[NSDictionary class]]                || [objisKindOfClass:[NSSet class]]) {        [descappendFormat:@"%@\t%@ = %@,\n", tab, key, [objdescriptionWithLocale:localeindent:level + 1]];      } else {        [descappendFormat:@"%@\t%@ = %@,\n", tab, key, obj];      }    }        [descappendFormat:@"%@}", tab];        return desc;  }   字典打印出来的格式是{}这样的结构,我们一样要考虑嵌套情况,并且根据嵌套添加对应的级别缩进。
测试
我们来测试一下效果,我们打印如下一个字典:
     NSMutableSet *set = [NSMutableSetsetWithArray:@[@"可变集合", @"字典->不可变集合->可变集合"]];    NSDictionary *dict = @{@"name"  : @"标哥的技术博客",                          @"title" : @"http://www.henishuo.com",                          @"count" : @(11),                          @"results" : [NSSetsetWithObjects:@"集合值1", @"集合值2", set , nil],                          @"summaries" : @[@"sm1", @"sm2", @{@"keysm": @{@"stkey": @"字典->数组->字典->字典"}}],                          @"parameters" : @{@"key1" : @"value1", @"key2": @{@"key11" : @"value11", @"key12" : @[@"三层", @"字典->字典->数组"]}},                          @"hasBug": @[@"YES",@"NO"],                          @"contact" : @[@"关注博客地址:http://www.henishuo.com", @"QQ群: 324400294", @"关注微博:标哥Jacky", @"关注GITHUB:CoderJackyHuang"]};    NSLog(@"%@", dict);   打印效果如下:
 
如何使用
首先要得到源代码,安装方式有两种,一种是直接使用 Cocoapods :
pod 'HYBUnicodeReadable', '~> 0.0.1'
或者直接下载源代码 https://github.com/CoderJackyHuang/HYBUnicodeReadable ,放入工程中即可
温馨提示:不需要引入头文件,即可达到效果!
写在最后
如果在使用过程中出现任何bug,请反馈作者以便及时修正!谢谢您的支持!
如果觉得赞,请给一个star
关注我
如果在使用过程中遇到问题,或者想要与我交流,可加入有问必答 QQ群: 324400294
关注微信公众号: iOSDevShares
关注新浪微博账号:标哥Jacky
支持并捐助
如果您觉得文章对您很有帮助,希望得到您的支持。您的捐肋将会给予我最大的鼓励,感谢您的支持!
| 支付宝捐助 | 微信捐助 | 
|---|---|
   |      |