Let's say I have an User object, and an user belongs to a group.
@interface User : NSObject
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (strong, nonatomic) Group *group;
@end
@interface Group : NSObject
@property (strong, nonatomic) NSString *name;
@end
Now, what I'd like to be able to do is something like this:
@implementation User
- (instancetype)initWithDict:(NSDictionary *)dict {
self = [super init];
KZPropertyMapper mapValuesFrom:dict toInstance:self usingMapping:@{
@"name": KZProperty(firstName),
@"lastname": KZProperty(lastName),
@"group": KZCall(Group, groupWithDict:, group),
}];
return self;
}
@end
So, call [Group groupWithDict:dict] and save that result to self.group. However, I now constantly have to create an intermediary helper method on the object I'm parsing, so the user object now gets something like this:
- (id)groupWithDict:(NSDictionary *)dict {
return [Group groupWithDict:dict];
}
After writing a bunch of classes like this, it gets really tedious to duplicate these methods like this. Is there any solution? It looked like KZCallT would be handy, but the property then needs to be on the target object instead of self.
Let's say I have an User object, and an user belongs to a group.
Now, what I'd like to be able to do is something like this:
So, call
[Group groupWithDict:dict]and save that result toself.group. However, I now constantly have to create an intermediary helper method on the object I'm parsing, so the user object now gets something like this:After writing a bunch of classes like this, it gets really tedious to duplicate these methods like this. Is there any solution? It looked like
KZCallTwould be handy, but the property then needs to be on the target object instead of self.