1

I'm using Mantle and it fits my basic need. After declaring JSONKeyPathsByPropertyKey and some PropertyJSONTransformer, I can turn a JSON Dictionary to an object

ETPUser *user = [MTLJSONAdapter modelOfClass:[ETPUser class] fromJSONDictionary:jsonDict error:nil];

Now I want to hook into this transformation process to do my other complex fields (setting other properties that are not declared in JSONKeyPathsByPropertyKey) in the jsonDict, but can't find any ways to do this

How to hook into Mantle ?

onmyway133
  • 45,645
  • 31
  • 257
  • 263
  • So you want to set default values when your user model is initialised? – David Snabel-Caunt Apr 23 '14 at 13:13
  • @DavidCaunt I want to do additional steps for other properties beside the properties that I declare in JSONKeyPathsByPropertyKey – onmyway133 Apr 23 '14 at 13:44
  • 1
    Does [this answer](http://stackoverflow.com/questions/23064204/how-to-set-default-value-in-ios-mantle-model-subclass/23245115#23245115) help? – David Snabel-Caunt Apr 23 '14 at 14:09
  • @DavidCaunt Look at the **dictionaryValue** in initWithDictionary, it is a **simplified** dictionary (only contains keys declared in JSONKeyPathsByPropertyKey), but the original dictionary (got from server API endpoint) contains more keys than that – onmyway133 Apr 24 '14 at 03:25
  • 1
    I'm sorry, I really don't understand what it is that you're trying to do. Please post some example JSON, the code for ETPUser, and the desired result. – David Snabel-Caunt Apr 24 '14 at 08:42

1 Answers1

0

Try with this snippet:

- (id)initWithDictionary:(NSDictionary *)dictionary error:(NSError *__autoreleasing *)error {

    NSDictionary *hookedValue = [[NSDictionary alloc] initWithObjectsAndKeys:
                                @"defaultValue1", @"defaultKey1",
                                @"defaultValue2", @"defaultKey2",
                                @"defaultValue3", @"defaultKey3",
                                nil];

    dictionary = [hookedValue mtl_dictionaryByAddingEntriesFromDictionary:dictionary];
    return [super initWithDictionary:dictionary error:error];
}
Andr3a88
  • 679
  • 5
  • 13