ios Mantle-正确覆盖默认initWithDictionary

ios Mantle-正确覆盖默认initWithDictionary,ios,github-mantle,Ios,Github Mantle,我面临以下问题 我有一个班级菜单和一个项目。菜单就像餐厅的菜单,有多个类别(如开胃菜、沙拉等),每个菜单都有多个相关项目。所以Menu.h有一个名为itemList的NSArray属性。我正在尝试使用Mantle自动加载这些对象 Menu.h @interface Menu : MTLModel <MTLJSONSerializing> @property (nonatomic) NSArray *itemList; @end 及 我的问题是:如果itemList为null,也就

我面临以下问题

我有一个班级菜单和一个项目。菜单就像餐厅的菜单,有多个类别(如开胃菜、沙拉等),每个菜单都有多个相关项目。所以Menu.h有一个名为itemList的NSArray属性。我正在尝试使用Mantle自动加载这些对象

Menu.h

@interface Menu : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSArray *itemList;
@end


我的问题是:如果itemList为null,也就是说,来自服务器的itemList响应为null,那么我想覆盖默认的initWithDictionary行为来做一些事情,并从Item.h的构造函数返回一个非NIL对象。我该怎么做?令我惊讶的是,代码没有到达这个构造函数,因为在形成Menu.h时它是空的。。我也指定了(NSValueTransformer)。。有线索吗?谢谢

如果JSON中
itemList
null
,Mantle将不会调用您的转换器,因此永远不会调用
Item
的初始值设定项

您可以通过如下方式更改
菜单
模型来指定默认值:

- (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];
}

你能添加你用来创建这些对象的代码吗?你好,大卫阿姨,谢谢你的关注。我调用后端RESTAPI以获取名为responseObject的JSON字典。然后,我创建了完整的菜单,其中包含itemList和进一步的链接
self.menuList=[MTLJSONAdapter modelsOfClass:[Menu class]fromJSONArray:responseObject error:nil]
Here menuList是餐厅(对于此特定应用程序)菜单项的完整列表,实际的json位于此处。。www.grubble.clipr.me/api/v1/restaurant/menu/?restaurant=2任何一家?我还有很多关于mantle的问题,真的在想我是否应该继续使用它。谢谢David,这很有帮助!关于Mantle,我还有几个问题要问,我自己会努力解决,否则我会在今晚再问:)
Item.m

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error {
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) {
      //DO SOMETHING WITH SELF AND RETURN A NON NIL OBJECT
      return self;
    } 
    return self;
}
- (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];
}