在 Objective-C 中使用 RestKit 实现对象映射
我在使用 RestKit 和 Objective-C 将 JSON 响应映射到对象时遇到问题。
我按照下面的示例在我的控制器中调用我的后端。
我遇到两个问题:
- [[RKObjectManager shareManager] postObject:request mapResponseWith:responseMapping delegate:self]; } <-- 这会导致“self”与类型 id 不兼容。我需要向此发送什么?
- 如何将 didLoadObject 中的结果转换为我定义的翻译对象 (translationText)
任何帮助将不胜感激。
@synthesize inputtext = _text;
@synthesize translation = _translation;
@synthesize translatedText = _translatedText;
- (Translation *)translatedText {
if (!_translatedText) _translatedText = [[Translation alloc] init];
return _translatedText; }
- (IBAction)translatePressed {
//create TranslationRequest
TranslationRequest *request = [[TranslationRequest alloc] init];
[request setSourceId:@"1"];
[request setRegionTag:@"Hello"];
[request setInputString:self.inputtext.text];
//fetch the desired mapping to map response with
RKObjectMapping * responseMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Translation class]];
[[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self]; }
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
self.translation.text = [object translatedText].translation;
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"Hit error: %@", error);
}
I am having trouble mapping a JSON response to objects using RestKit and Objective-C.
I have already set up my RKObjectManager and mappings in my AppDelegate as suggested in my previous post by mja.
I call my backend in my controller as per the example below.
There are two issues I'm having trouble with:
- [[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self]; } <-- This results in a "self" is incompatible with type id. What do I need to send to this?
- How do I cast the result in didLoadObject to the Translation object I've defined (translationText)
Any help would be much appreciated.
@synthesize inputtext = _text;
@synthesize translation = _translation;
@synthesize translatedText = _translatedText;
- (Translation *)translatedText {
if (!_translatedText) _translatedText = [[Translation alloc] init];
return _translatedText; }
- (IBAction)translatePressed {
//create TranslationRequest
TranslationRequest *request = [[TranslationRequest alloc] init];
[request setSourceId:@"1"];
[request setRegionTag:@"Hello"];
[request setInputString:self.inputtext.text];
//fetch the desired mapping to map response with
RKObjectMapping * responseMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Translation class]];
[[RKObjectManager sharedManager] postObject:request mapResponseWith:responseMapping delegate:self]; }
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object {
self.translation.text = [object translatedText].translation;
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"Hit error: %@", error);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要纠正第一个问题,请在 .h 文件中声明您的控制器,如下所示:
您可以像这样进行转换:
或者您可以通过调用适当的选择器来避免转换,
您可以使用 Translation 对象中的 @properties 定义来更新您的问题为了确保这个答案是正确的。
to rectify the first issue, declare your controler in the .h file as follows:
You cast it just like this:
or you can avoid the cast by calling appropriate selector
you may update your question with the definition of @properties in your Translation object in order to be sure this answer is correct.