Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KZCall on different object? #41

Open
kevinrenskers opened this issue Nov 3, 2014 · 4 comments
Open

KZCall on different object? #41

kevinrenskers opened this issue Nov 3, 2014 · 4 comments

Comments

@kevinrenskers
Copy link

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.

@krzysztofzablocki
Copy link
Owner

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:
#41

@kevinrenskers
Copy link
Author

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?

@krzysztofzablocki
Copy link
Owner

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:
#41 (comment)

@tobihagemann
Copy link

+1 KZCallClass would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants