PropertyMapper icon indicating copy to clipboard operation
PropertyMapper copied to clipboard

KZCall on different object?

Open kevinrenskers opened this issue 10 years ago • 4 comments

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.

kevinrenskers avatar Nov 03 '14 16:11 kevinrenskers

For global/shared support I'd use boxing not KZCall, Adding boxValueAsGroup: as a category on KZPM and then use KZBox(Group, property)

Readme should have an example about that, does that approach seem better?

Krzysztof Zabłocki Co-Founder Pixle.pl

On Mon, Nov 3, 2014 at 5:21 PM, Kevin Renskers [email protected] wrote:

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, groupsWithDict:, group),
    }];
    return self;
}
@end

So, call [Group groupsWithDict: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)groupsWithDict:(NSDictionary *)dict {
    return [Group groupsWithDict: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.

Reply to this email directly or view it on GitHub: https://github.com/krzysztofzablocki/KZPropertyMapper/issues/41

krzysztofzablocki avatar Nov 03 '14 16:11 krzysztofzablocki

Well, it would at least make it global and reusable, but it would still mean duplicating a method to a boxing method. It's not ideal. Something like KZCall(Group, groupWithDict:, group) would seems to be the clearest thing? Would this be hard to add?

kevinrenskers avatar Nov 03 '14 16:11 kevinrenskers

Should be straight forward, we'd probably need to add KZCallClass or similar because target and called will differ then

Krzysztof Zabłocki Co-Founder Pixle.pl

On Mon, Nov 3, 2014 at 5:31 PM, Kevin Renskers [email protected] wrote:

Well, it would at least make it global and reusable, but it would still mean duplicating a method to a boxing method. It's not ideal. Something like KZCall(Group, groupWithDict:, group) would seems to be the clearest thing? Would this be hard to add?

Reply to this email directly or view it on GitHub: https://github.com/krzysztofzablocki/KZPropertyMapper/issues/41#issuecomment-61504856

krzysztofzablocki avatar Nov 03 '14 16:11 krzysztofzablocki

+1 KZCallClass would be great!

tobihagemann avatar Jun 05 '15 10:06 tobihagemann