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

1589

被折叠的 条评论
为什么被折叠?



