Ios 如何将关系添加到具有Mantle的父对象?

Ios 如何将关系添加到具有Mantle的父对象?,ios,objective-c,json,github-mantle,Ios,Objective C,Json,Github Mantle,我有这样的父/子类: @interface Parent : MTLModel <MTLJSONSerializing> - (void)someMethod; @property a,b,c...; // from the JSON @property NSArray *childs; // from the JSON @end @interface Child : MTLModel <MTLJSONSerializing> @property d,e

我有这样的父/子类:

@interface Parent : MTLModel <MTLJSONSerializing>
- (void)someMethod;

@property a,b,c...;   // from the JSON    
@property NSArray *childs;  // from the JSON
@end

@interface Child : MTLModel <MTLJSONSerializing>
@property d,e,f,...;    // from the JSON    
@property Parent *parent;   // *not* in the JSON
@end
我该如何使用Mantle

我希望,当父对象解析子对象的JSON并创建子对象类时,向其自身添加一个引用。(使用init方法??)


谢谢

我通过重写
MTLModel-initWithDictionary:error:
方法来实现这一点。像这样的

子接口:

@interface BRPerson : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy, readonly) NSString *name;
@property (strong, nonatomic) BRGroup *group; // parent
@end
技术说明:


如果你像我一样好奇的话,我已经尝试通过更改其
forwardBlock
reversibleBlock
来调整
MTLJSONAdapter
。但是我不能,因为它们在
MTLReversibleValueTransformer
超类中,并且该类在的“MTLValueTransformer.m”中私下声明。因此上面的
initWithDictionary
方法应该更简单。

您必须手动完成。
@interface BRPerson : MTLModel <MTLJSONSerializing>
@property (nonatomic, copy, readonly) NSString *name;
@property (strong, nonatomic) BRGroup *group; // parent
@end
- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error {
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) return nil;

    // iterates through each child and set its parent
    for (BRPerson *person in self.people) {
        person.group = self;
    }
    return self;
}