在mja的帮助下,我成功地使用RestKit和Objective-C建立了简单的对象映射。请参阅我之前的问题here。
我的下一步是尝试以相同的方式处理嵌套的JSON。
我的JSON看起来像这样,外部是带有一些嵌套'投票'的CandidatePhrase:
{ "Id":33696,
"Phrase": "phrase",
"BadCount":0,
"Votes":[{"Id":447,"OriginalId":33696,"Votes":2,"Translation":"translation 1"},
{"Id":746,"OriginalId":33696,"Votes":1,"Translation":"translation 2"},
{"Id":747,"OriginalId":33696,"Votes":1,"Translation":"translation 3"}
]}
我在AppDelegate中创建了一个关系,如下所示:
[candidatePhraseMapping mapKeyPath:@"votes" toRelationship:@"vote" withMapping:voteMapping];
当我在控制器中调用make我的请求时,我能够处理其余的CandidatePhrase,但是我不确定如何将嵌套的'Vote'对象映射到数组中以便我可以在一个TableView
(像这样的伪代码......)
// Store the votes in an array
_votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
这是我的CandidatePhrase对象
@interface CandidatePhrase : NSObject
@property (nonatomic, retain) NSNumber* ident;
@property (nonatomic, retain) NSNumber* badcount;
@property (nonatomic, retain) NSString* phrase;
@property (nonatomic, retain) NSArray* votes;
@end
和我的投票对象
@interface Vote : NSObject
@property (nonatomic, retain) NSNumber* ident;
@property (nonatomic, retain) NSNumber* originalId;
@property (nonatomic, retain) NSNumber* votecount;
@property (nonatomic, retain) NSString* translation;
+ (id)voteWithTranslationId:(NSNumber *)ident translation:(NSString *)translation;
@end
非常感谢任何帮助。
以下是我的映射代码
// Votes Mapping
RKObjectMapping* voteMapping = [RKObjectMapping mappingForClass:[Vote class]];
[voteMapping mapKeyPath:@"Id" toAttribute:@"ident"];
[voteMapping mapKeyPath:@"OriginalId" toAttribute:@"originalId"];
[voteMapping mapKeyPath:@"Votes" toAttribute:@"votecount"];
[voteMapping mapKeyPath:@"Translation" toAttribute:@"translation"];
[[manager mappingProvider] addObjectMapping:voteMapping];
// Candidate Phrase Mapping
RKObjectMapping *candidatePhraseMapping = [RKObjectMapping mappingForClass:[CandidatePhrase class]];
[candidatePhraseMapping mapKeyPath:@"Id" toAttribute:@"ident"];
[candidatePhraseMapping mapKeyPath:@"Phrase" toAttribute:@"phrase"];
[candidatePhraseMapping mapKeyPath:@"BadCount" toAttribute:@"badcount"];
[candidatePhraseMapping mapKeyPath:@"Votes" toRelationship:@"votes" withMapping:voteMapping];
[[manager mappingProvider] addObjectMapping:candidatePhraseMapping];
为清楚起见,这里是我试图访问控制器上的投票项目
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object
{
CandidatePhrase *myCandidatePhrase = (CandidatePhrase*)object;
self.candidateText.text = myCandidatePhrase.phrase; <-- works fine
_votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
for (id o2 in _votes) {
//Vote *vote = o2;
NSLog(@"Item name: %@", o2); <-- sees object but crashes
}
NSLog(@"Votes: %@", myCandidatePhrase.votes);
_votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
[_votesTableView reloadData];
}
我的表与
绑定Vote *vote = [_votes objectAtIndex:indexPath.row];
cell.textLabel.text = vote.translation;
答案 0 :(得分:3)
您无需手动管理嵌套的NSArray。我相信问题可能只是一个简单的错字,因为你映射keyPath“投票”,但你的json包含大写“V”的“投票”。
[candidatePhraseMapping mapKeyPath:@"Votes" toRelationship:@"votes" withMapping:voteMapping];
如果这样做无效,请随时发表评论并使用voteMapping
更新您的问题。
此外,didLoadObject的内容可以简化:
//in .h file
@property (nonatomic, retain) NSArray* votes;
// in implementation
@synthesize votes;
...
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object
{
CandidatePhrase *myCandidatePhrase = (CandidatePhrase*)object;
self.candidateText.text = myCandidatePhrase.phrase;
self.votes = myCandidatePhrase.votes;
}