0

在 RestKit 中是否有一种简单的方法可以在子对象中包含父引用?例如,如果我有如下所示的 JSON:

{
    "parent": {
        "name": "Jim",
        "child": {
            "name": "John"
        }
    }
}

如何配置 RKObjectMapping 以使我的“子”对象具有“父”引用(即 child.parent)?

4

1 回答 1

0

我想出了一个不错的解决方法,因为这似乎不受 RestKit 直接支持。

您需要做的是使用键值验证将父引用放入子对象中。因此,对于上面的示例,父模型看起来像这样:

@interface Parent : NSObject

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) Child *child;

@end

@implementation Parent

@synthesize name = _name;
@synthesize child = _child;

- (BOOL)validateChild:(id *)ioValue error:(NSError **)outError
{
    Child *child = (Child *)*ioValue;
    child.parent = self;
    return YES;
}

@end
于 2012-08-07T09:01:15.257 回答