在 Objective-C 中使用 RestKit 实现对象映射

发布于 2024-12-19 04:56:39 字数 1680 浏览 1 评论 0原文

我在使用 RestKit 和 Objective-C 将 JSON 响应映射到对象时遇到问题。

我已经按照 我之前的帖子,作者:mja

我按照下面的示例在我的控制器中调用我的后端。

我遇到两个问题:

  1. [[RKObjectManager shareManager] postObject:request mapResponseWith:responseMapping delegate:self]; } <-- 这会导致“self”与类型 id 不兼容。我需要向此发送什么?
  2. 如何将 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:

  1. [[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?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

婴鹅 2024-12-26 04:56:39

要纠正第一个问题,请在 .h 文件中声明您的控制器,如下所示:

#import "RestKit/RestKit.h"
...
@interface MyController : UIViewController<RKObjectLoaderDelegate>

您可以像这样进行转换:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object  { 
    Translation *myTranslation = (Translation*)object;
    ... 
} 

或者您可以通过调用适当的选择器来避免转换,

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object  { 
    self.translation.text = [[object translatedText] translation];
} 

您可以使用 Translation 对象中的 @properties 定义来更新您的问题为了确保这个答案是正确的。

to rectify the first issue, declare your controler in the .h file as follows:

#import "RestKit/RestKit.h"
...
@interface MyController : UIViewController<RKObjectLoaderDelegate>

You cast it just like this:

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object  { 
    Translation *myTranslation = (Translation*)object;
    ... 
} 

or you can avoid the cast by calling appropriate selector

- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object  { 
    self.translation.text = [[object translatedText] translation];
} 

you may update your question with the definition of @properties in your Translation object in order to be sure this answer is correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文