使用Objective-C在Xcode中使用RestKit映射嵌套对象

使用Objective-C在Xcode中使用RestKit映射嵌套对象,objective-c,xcode,restkit,Objective C,Xcode,Restkit,在mja的帮助下,我成功地使用RestKit和Objective-C设置了简单的对象映射。请看我前面的问题 我的下一步是尝试以同样的方式处理嵌套JSON 我的JSON看起来像这样,外部是一个带有嵌套“投票”的候选短语: { "Id":33696, "Phrase": "phrase", "BadCount":0, "Votes":[{"Id":447,"OriginalId":33696,"Votes":2,"Translation":"translation 1"}, {"Id":746,"O

在mja的帮助下,我成功地使用RestKit和Objective-C设置了简单的对象映射。请看我前面的问题

我的下一步是尝试以同样的方式处理嵌套JSON

我的JSON看起来像这样,外部是一个带有嵌套“投票”的候选短语:

{ "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];
当我在我的控制器中调用makerequest时,我可以处理CandidatePhrase的其余部分,但我不确定如何将嵌套的“Vote”对象映射到数组中,以便在TableView中使用它们

(类似这样的伪代码…)

这是我的候选短语对象

@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
和我的投票对象

@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];
 } 

您不需要手动管理嵌套的NSArray。我相信问题可能只是一个简单的输入错误,因为您映射了keyPath“投票”,但json包含大写字母“V”的“投票”

如果这没有帮助,请随时留下评论,并使用
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;
}

谢谢你。我更新了上面的代码,并得到错误“NSUnknownKeyException”,原因:“[valueForUndefinedKey:]:此类不符合关键投票的键值编码。如果这有帮助,我还更新了访问另一端投票属性的代码。您好,我想这行是问题所在_投票=[[NSArray alloc]initWithObjects:myCandidatePhrase.vows,nil];myCandidatePhrase.vows本身就是一个数组。你想拥有一个只有一个对象的数组吗?作为投票数组?阅读你的上一次编辑,显然不是。为什么不保留整个(CandidatePhrase*)对象;在一处房产里?self.candidateText属性是什么类型的?不客气。我还更新了我的答案,并从我的评论中得到了提示。
//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;
}