如何在iOS Mantle模型子类中设置默认值

如何在iOS Mantle模型子类中设置默认值,第1张

概述@interface Entity () @property (assign) int searchTotalPagesAll; @property (assign) int searchTotalPagesIdeas;@end@implementation Entity + (NSDictionary *)JSONKeyPathsByPropertyKey {return
@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)以获取默认值?此模型具有需要此内容和其他几个属性的内部方法.

解决方法 无论您是使用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];}
总结

以上是内存溢出为你收集整理的如何在iOS Mantle模型子类中设置默认值全部内容,希望文章能够帮你解决如何在iOS Mantle模型子类中设置默认值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1102773.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存