NSLog打印查年数组字典对象的中文信息

当使用NSLog打印NSArray、NSDictionary、NSObject含有中文的详细信息时,会出现显示异常。为解决此问题,可以创建这些类的分类并重写相应方法。具体实现包括对NSArray和NSDictionary创建分类以自定义打印方法,以及对NSObject创建分类来处理中文信息的正确显示。

使用NSLog查看信息时,如果是NSArray、NSDictionary、NSObject的对象时,无法查看到详细的信息,特别是中文时,无法正常显示,这时候需要进行特殊设置。
1、对于NSArray、NSDictionary时,需要创建分类,并重写方法- (NSString *)descriptionWithLocale:(id)locale实现;
2、对于NSObject时,需要创建分类,重写方法- (NSString *)description实现。

实现代码

NSArray

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSArray (Category)

@end

NS_ASSUME_NONNULL_END

#import "NSArray+Category.h"

@implementation NSArray (Category)

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

@end

结果

NSArray *array = @[@"张三", @"李四", @"wangWu", @"小明"];
NSLog(@"array: %@", array);

2019-04-17 17:40:27.706032+0800 DemoLog[20566:576588] array: (
	张三,
	李四,
	wangWu,
	小明,
)

NSDictionary

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSDictionary (Category)

@end

NS_ASSUME_NONNULL_END


#import "NSDictionary+Category.h"

@implementation NSDictionary (Category)

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

@end

结果

NSDictionary *dict = @{@"姓名":@"张三", @"职业":@"农二代", @"年龄":@(30), @"company":@"个体"};
NSLog(@"dict: %@", dict);
    
2019-04-17 17:40:27.706222+0800 DemoLog[20566:576588] dict: {
	姓名 = 张三;
	职业 = 农二代;
	年龄 = 30;
	company = 个体;
}

NSObject

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSObject (Category)

@end

NS_ASSUME_NONNULL_END


#import "NSObject+Category.h"
#import <objc/runtime.h>

@implementation NSObject (Category)

- (NSString *)description
{
    NSString *desc = @"\n{";
    //
    unsigned int outCount;
    objc_property_t *properties = class_copyPropertyList([self class], &outCount);
    for (int i = 0; i < outCount; i ++) {
        objc_property_t property = properties[i];
        //获取property的C字符串
        const char * propName = property_getName(property);
        if (propName) {
            //获取NSString类型的property名字
            NSString *prop = [NSString stringWithCString:propName encoding:[NSString defaultCStringEncoding]];
            //获取property对应的值
            id obj = [self valueForKey:prop];
            //将属性名和属性值拼接起来
            desc = [desc stringByAppendingFormat:@"%@: %@,\n",prop,obj];
        }
    }
    desc = [desc stringByAppendingFormat:@"}\n"];
    free(properties);
    
    return desc;
}

@end

结果

Person *person = [[Person alloc] init];
person.name = @"小明";
person.job = @"研发工程师";
person.age = @"28";
person.company = @"BYD Auto";
person.project = @[@"project1", @"王者荣耀", @"跑跑卡丁车", @"逃离神庙", @"吃鸡"];
person.learn = @{@"开发":@"Objective-C", @"project":@(10), @"team":@[@"张三", @"李四", @"wangWu", @"小明"]};
NSLog(@"person: %@", person);

2019-04-17 17:40:27.706527+0800 DemoLog[20566:576588] person: 
{name: 小明,
age: 28,
job: 研发工程师,
company: BYD Auto,
project: (
	project1,
	王者荣耀,
	跑跑卡丁车,
	逃离神庙,
	吃鸡,
),
learn: {
	project = 10;
	team = (
	张三,
	李四,
	wangWu,
	小明,
);
	开发 = Objective-C;
},
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

番薯大佬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值