iOS工具 - NSLog日志打印出中文

2021/11/21 6:09:44

本文主要是介绍iOS工具 - NSLog日志打印出中文,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

NSLog 日志打印出中文

1 - NSLog日志可以正常打印出中文,而不被编译成其它字符,对字典进行扩展即可

2 - 代码示例

// - NSDictionary+Log.m

 1 #import "NSDictionary+Log.h"
 2 @implementation NSDictionary (Log)
 3 - (NSString *)descriptionWithLocale:(id)locale {
 4     
 5     NSMutableString *string = [NSMutableString string];
 6     
 7     // 开头有个{
 8     [string appendString:@"{\n"];
 9     
10     // 遍历所有的键值对
11     [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
12         [string appendFormat:@"\t%@", key];
13         [string appendString:@" : "];
14         [string appendFormat:@"%@,\n", obj];
15     }];
16     
17     // 结尾有个}
18     [string appendString:@"}"];
19     
20     // 查找最后一个逗号
21     NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
22     if (range.location != NSNotFound)
23         [string deleteCharactersInRange:range];
24     
25     return string;
26 }
27 @end
28 
29 @implementation NSArray (Log)
30 
31 - (NSString *)descriptionWithLocale:(id)locale
32 {
33     NSMutableString *string = [NSMutableString string];
34     
35     // 开头有个[
36     [string appendString:@"[\n"];
37     
38     // 遍历所有的元素
39     [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
40         [string appendFormat:@"\t%@,\n", obj];
41     }];
42     
43     // 结尾有个]
44     [string appendString:@"]"];
45     
46     // 查找最后一个逗号
47     NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
48     if (range.location != NSNotFound)
49         [string deleteCharactersInRange:range];
50     
51     return string;
52 }
53 
54 @end

 



这篇关于iOS工具 - NSLog日志打印出中文的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程