ios Mantle - Overriding default initWithDictionary properly -


i facing following problem..

i have class menu.h , item.h. menu menu of restaurant , has multiple categories (like appetizers, salads, etc) , each menu has multiple items associated. menu.h has nsarray property called itemlist. trying automatically load these objects using mantle.

menu.h  @interface menu : mtlmodel <mtljsonserializing> @property (nonatomic) nsarray *itemlist; @end 

and

menu.m  @implementation menu + (nsdictionary *)jsonkeypathsbypropertykey {     // model_property_name : json_field_name     return @{              }; }  + (nsvaluetransformer *)itemlistjsontransformer {     return [nsvaluetransformer mtl_jsonarraytransformerwithmodelclass: item.class]; }  - (instancetype)initwithdictionary:(nsdictionary *)dictionaryvalue error:(nserror **)error {     self = [super initwithdictionary:dictionaryvalue error:error];     if (self == nil) return nil;     return self; } 

and

item.m  - (instancetype)initwithdictionary:(nsdictionary *)dictionaryvalue error:(nserror **)error {     self = [super initwithdictionary:dictionaryvalue error:error];     if (self == nil) {       //do self , return non nil object       return self;     }      return self; } 

my question following: if itemlist null, i.e., server null response comes itemlist, , want override default initwithdictionary behavior , return non nil object constructor of item.h how do it? code not reaching constructor surprise, because null when menu.h being formed.. did specify (nsvaluetransformer) well.. leads ? thanks!

if itemlist null in json, mantle won't call transformer, item's initializer never called.

you can specify default changing menu model this:

- (instancetype)initwithdictionary:(nsdictionary *)dictionaryvalue error:(nserror *__autoreleasing *)error {     // create itemlistdefault value.       nsdictionary *defaults = @{         @"itemlist" : itemlistdefault     };     dictionaryvalue = [defaults mtl_dictionarybyaddingentriesfromdictionary:dictionaryvalue];     return [super initwithdictionary:dictionaryvalue error:error]; } 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -