2

我已经定义了一个非常简单的映射,具有一些普通属性,但是现在我遇到了我在服务器上的数据结构是一棵树的问题,所以我得到了一个“CustomObject”列表,其中包含一些属性和一个“CustomObject”列表“ 哪个 ...

所以在代码中它看起来像这样(简化)

+ (RKObjectMapping*)getCustomObjectMapping
{
    RKObjectMapping* customObjectMapping = [RKObjectMapping mappingForClass:[CustomObject class]];
    [customObjectMapping mapKeyPath:@"title" toAttribute:@"title"];
    [..]

    // Define the relationship mapping
    //[customObjectMapping mapKeyPath:@"nextLevel" toRelationship:@"nexLevel" withMapping:[self getCustomObjectMapping]];

    return customObjectMapping;
}

这显然会导致无休止的递归。

有没有一种聪明的方法来做这个映射?

4

1 回答 1

7

它非常简单,最近的 RestKit 库支持它就好了。您需要引用正在构建的映射对象,而不是递归引用 +getCustomObjectMapping。以下是您需要执行的操作:

+ (RKObjectMapping*)getCustomObjectMapping
{
    RKObjectMapping* customObjectMapping = [RKObjectMapping mappingForClass:[CustomObject class]];
    [customObjectMapping mapKeyPath:@"title" toAttribute:@"title"];
    [..]

    // Define the relationship mapping
    [customObjectMapping mapKeyPath:@"nextLevel" toRelationship:@"nexLevel" withMapping:customObjectMapping];

    return customObjectMapping;
}
于 2013-04-12T21:03:14.883 回答