使用RestKit / iOS映射嵌套数组

时间:2012-10-21 02:39:50

标签: objective-c ios json ios6 restkit

我需要用RestKit映射一个嵌套数组,我差不多了(我想)。

我需要映射的JSON,看起来像这样

[
    {
        _id: "5058670183970a0002000450",
        app_store_url: "http://itunes.apple.com/app/culturonda-alto-adige-sudtirol/id534467629",
        comments: [
            {
                _id: "5058670783970a0002000456",
            },
            {
                _id: "50588d7f83970a000200065c",
            }
        ]
    }
]

我存储信息的控制器如下所示:

#import <Foundation/Foundation.h>
#import "CommentData.h"

@interface DesignData : NSObject
    @property (retain, nonatomic) NSString *designId;
    @property (retain, nonatomic) NSSet *commentsRelationship;
@end

#import <Foundation/Foundation.h>

@interface CommentData : NSObject
    @property (retain, nonatomic) NSString *commentId;
@end

我的映射如下所示:

RKObjectManager *objectManager = [RKObjectManager sharedManager];

RKObjectMapping *commentsMapping = [RKObjectMapping mappingForClass:[CommentData class]];
[commentsMapping mapKeyPathsToAttributes:@"_id", @"commentId", nil];

RKObjectMapping *designMapping = [RKObjectMapping mappingForClass:[DesignData class] ];
[designMapping mapKeyPathsToAttributes:@"_id", @"designId", nil];

[designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping];

[objectManager.mappingProvider setMapping:designMapping forKeyPath:@""];

注释进入数组,但它不会存储在注释控制器中。如果我在objecloader回调中显示注释,如下所示:

DesignData *designData = [objects objectAtIndex:0];
NSLog(@"Loaded %@ ", designData.commentsRelationship);
它给了我:

2012-10-20 22:37:55.258 RestKitTest5[4144:c07] Loaded {(
                                                        <CommentData: 0x8689730>,
                                                        <CommentData: 0x868bed0>,
                                                        <CommentData: 0x868bfe0>,
                                                        <CommentData: 0x868bf50>
                                                        )}

这是一个开始。但我做不到:

DesignData *designData = [objects objectAtIndex:0];
NSLog(@"Loaded %@ ", designData.commentsRelationship.commentId);

那么如何将整个事物存储为数组呢?

2 个答案:

答案 0 :(得分:2)

我替换了这一行后,我为我工作了:

[designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping];

使用:

[designMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments" toKeyPath:@"commentsRelationship" withMapping: commentsMapping]];

答案 1 :(得分:1)

我已经做得对了,我最后只是愚蠢。

我得到了数组,数组中的对象是正确的类型(CommentData),所以我所要做的就是循环遍历数组,从那里我可以分别处理每个对象。