4

我需要用 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);

那么如何将整个内容存储为数组呢?

4

2 回答 2

2

替换这条线后,我为我工作:

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

和:

[designMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments" toKeyPath:@"commentsRelationship" withMapping: commentsMapping]];
于 2016-12-21T14:30:52.220 回答
1

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

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

于 2012-10-21T03:29:17.687 回答