Skip to content

KZCall on different object? #41

Description

@kevinrenskers

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions