我需要用 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);
那么如何将整个内容存储为数组呢?