@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
由于mantle示例没有init方法,我应该在哪里初始化这些属性(searchTotalPagesAll,searchTotalPagesIdeas)以获取默认值?此模型具有需要此内容和其他几个属性的内部方法。
答案 0 :(得分:13)
无论您是使用JSON还是其他方式创建Mantle模型,都会使用[-initWithDictionary:error:]
初始化模型。在模型类中,您可以将默认值添加到用于初始化模型的值:
- (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];
}
答案 1 :(得分:9)
您可以在init方法中设置默认值。
- (instancetype)init
{
self = [super init];
if (self) {
self.searchTotalPagesAll = 1;
self.searchTotalPagesIdeas = 2;
}
return self;
}