objective c - How to set default value in iOS Mantle model subclass -
@interface entity () @property (assign) int searchtotalpagesall; @property (assign) int searchtotalpagesideas; @end @implementation entity + (nsdictionary *)jsonkeypathsbypropertykey { return @{ @"id": @"entity.id_entity", @"name": @"entity.name", @"coverage" : @"entity.coverage", @"id_city": @"entity.id_city", @"cityname":@"entity.city", @"countryname":@"entity.country", @"statename":@"entity.district", @"countrycode": @"entity.countrycode", @"keyword1": @"entity.key1", ... etc
since mantle examples doesn't have init method, should initialize properties (searchtotalpagesall, searchtotalpagesideas) default values ? model has internal methods need , several other properties.
whether create mantle model json or otherwise, model initialised [-initwithdictionary:error:]
. in model class, can add defaults values used initialise model:
- (instancetype)initwithdictionary:(nsdictionary *)dictionaryvalue error:(nserror *__autoreleasing *)error { nsdictionary *defaults = @{ @"searchtotalpagesall" : @(10), @"searchtotalpagesideas" : @(5) }; dictionaryvalue = [defaults mtl_dictionarybyaddingentriesfromdictionary:dictionaryvalue]; return [super initwithdictionary:dictionaryvalue error:error]; }
Comments
Post a Comment