1.为什么要为你的模型对象加上description方法
比如类Person
@interface Person : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age;
@property (nonatomic,strong) NSDate *birthday;
@end
@implementation Person
@end
我们平时将它的一个对象打印
Person *p = [[Person alloc] init];
p.name = @"aaa";
p.birthday = [NSDate date];
p.age = 18;
NSLog(@"%@",p);
然而打印的结果,却并不是我们想要的,我们更想要打印其中的各个属性的值
2016-08-16 11:38:27.860 Demo[30316:1805760] <Person: 0x7ff84b811ae0>
所以我们可以实现description方法
@implementation Person
-(NSString *)description{
return [NSString stringWithFormat:@"name : %@ age : %d ,birthday : %@",self.name,self.age,self.birthday];
}
@end
这样打印的结果如下
2016-08-16 11:53:06.024 Demo[31045:1826418]
name : aaa age : 18 ,birthday : 2016-08-16 03:53:06 +0000
看起来更加清晰了
2.使用NSDictionary来辅助实现description
也许description方法可以这样实现
@implementation Person
-(NSString *)description{
NSMutableDictionary *properties = [NSMutableDictionary dictionary];
properties[@"name"] = self.name;
properties[@"age"] = [NSNumber numberWithInt:self.age];
properties[@"birthday"] = self.birthday;
return [NSString stringWithFormat:@"%@",properties];
}
@end
这样打印出来就是这样
2016-08-16 11:59:14.195 Demo[31083:1830255] {
age = 18;
birthday = "2016-08-16 03:59:14 +0000";
name = aaa;
}
打印的整洁,易看,而且还不用写那么多String的格式语句。
3.使用runtime辅助实现
虽然借助NSDictionary可以方便一点,但是当成员变量增多的时候仍然需要一个一个加入到字典中,比较麻烦。所以我们可以借助runtime自动遍历类中的所有属性
@implementation NSObject (YXParams)
- (NSDictionary *)keyValues
{
NSMutableDictionary *props = [NSMutableDictionary dictionary];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
NSDictionary *values;
NSDictionary *propertyNames;
if ([self respondsToSelector:@selector(paramsValueReplace)]) {
values=[self paramsValueReplace];
}
if ([self respondsToSelector:@selector(paramsNamesReplace)]) {
propertyNames=[self paramsNamesReplace];
}
for (i = 0; i<outCount; i++)
{
objc_property_t property = properties[i];
const char* char_f =property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue;
if (values[propertyName]) {
propertyValue=values[propertyName];
}else{
propertyValue= [self valueForKey:(NSString *)propertyName];
}
if (propertyNames[propertyName]) {
propertyName=propertyNames[propertyName];
}
if (propertyValue) {
[props setObject:propertyValue forKey:propertyName];
}
}
free(properties);
return props;
}
end
这样description方法就变为
@implementation Person
- (NSString *)description{
return [NSString stringWithFormat:@"%@",[self keyValues]];
}
@end
这样是不是就很简单了?
4.防止无限递归
上面的方式虽然简单了,但是仍然会有问题,那就是当A有一个B的属性,B有一个A的属性的时候,调用A 或者 B的对象的打印,都会造成无限递归。比如:
Person:
@interface Person : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age;
@property (nonatomic,strong) NSDate *birthday;
@property (nonatomic,weak) Tool *tool;
@end
@implementation Person
- (NSString *)description{
return [NSString stringWithFormat:@"%@",[self keyValues]];
}
@end
Tool:
@interface Tool : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) float price;
@property (nonatomic,weak) Person *person;
@end
@implementation Tool
- (NSString *)description{
return [NSString stringWithFormat:@"%@",[self keyValues]];
}
@end
调用场景
Person *p = [[Person alloc] init];
p.name = @"aaa";
p.birthday = [NSDate date];
p.age = 18;
Tool *tool = [[Tool alloc] init];
tool.name = @"锤子";
tool.price = 11;
tool.person = p;
p.tool = tool;
NSLog(@"%@",p);
这样就会出现如下的递归错误:
递归错误
怎么解决呢?
5.在使用runtime获取属性的时候,进行过滤
这样keyValues方法就要改成如下的形式,通过一个block进行过滤
- (NSDictionary *)keyValuesWithFilter:(BOOL(^)(NSString *propertyName,id value))filter
{
NSMutableDictionary *props = [NSMutableDictionary dictionary];
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
NSDictionary *values;
NSDictionary *propertyNames;
if ([self respondsToSelector:@selector(paramsValueReplace)]) {
values=[self paramsValueReplace];
}
if ([self respondsToSelector:@selector(paramsNamesReplace)]) {
propertyNames=[self paramsNamesReplace];
}
for (i = 0; i<outCount; i++)
{
objc_property_t property = properties[i];
const char* char_f =property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue;
if (values[propertyName]) {
propertyValue=values[propertyName];
}else{
propertyValue= [self valueForKey:(NSString *)propertyName];
}
if (propertyNames[propertyName]) {
propertyName=propertyNames[propertyName];
}
if (propertyValue) {
//在此处利用block过滤一下
if(filter(propertyName,propertyValue))
[props setObject:propertyValue forKey:propertyName];
}
}
free(properties);
return props;
}
我们只需要把其中一个对象的description方法变成如下形式就好了,比如tool:
@implementation Tool
- (NSString *)description{
return [NSString stringWithFormat:@"%@",[self keyValuesWithFilter:^BOOL(NSString *propertyName, id value) {
if([value isKindOfClass:[Person class]]){
Person *p = (Person *)value;
if(p.tool == self){
return NO;
}
}
return YES;
}]];
}
@end
这样打印就不会出现无线递归错误了。
6.使用xCodeGenerateDescriptionPlugin插件自动生成description方法
上面的方法,我们还可以使用插件自动为类生成description方法
网友评论