委托不工作(Restkit 相关?)

发布于 2024-12-03 16:41:53 字数 2399 浏览 1 评论 0原文

我刚刚开始使用 Objective C 和 Restkit

我创建了一个示例应用程序,并在 MyAppDelegate 文件中添加了 RKRequestDelegate

@interface MyAppDelegate : NSObject <UIApplicationDelegate, RKRequestDelegate> {…

并添加

  RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
   [client get:@"/titles.json" delegate:self];

到 MyAppDelegate.m 中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

我还在 MyAppDelegate.m 中添加了一个方法

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    if ([request isGET]) {        
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
 }

到目前为止,一切正常,我看到我的 Rails 应用程序的输出结果!!!

由于这些东西不属于 MyAppDelegate.m 我正在将这些东西移动到我的模型中。在我的 Titles.h 中我添加了

@interface Titles : NSManagedObject <RKRequestDelegate> {

,在 Titles.m 中我添加了

+ (void) update {   
    [[RKClient sharedClient] get:@"/titles.json" delegate:self];
}

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    if ([request isGET]) {
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
}

在我的 MyAppDelegate.m 中我将 : 替换

 RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
   [client get:@"/titles.json" delegate:self];

  RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
  [Titles update];

,当我现在运行时,我没有得到任何输出。 我放置了几个断点,其中一个位于 RKRequest 文件中的 - (void)didFinishLoad:(RKResponse*)response 中 那里的 if 测试:

if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
        [_delegate request:self didLoadResponse:finalResponse];
    }

在我的第一次尝试中成功时失败(当所有内容都在 MyAppDelegate 中时)

我检查了调试器中的变量 _delate ,它说: _delegate = MyAppDelegate 在我的第一次尝试中, _delegate = Titles 在我的第二次尝试中(两者都喜欢它应该)

为什么respondsToSelector会失败? (委托是正确的,方法存在于标题中)

I'm just getting started with Objective C and Restkit

I created a sample application and added the RKRequestDelegate in MyAppDelegate file

@interface MyAppDelegate : NSObject <UIApplicationDelegate, RKRequestDelegate> {…

and added

  RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
   [client get:@"/titles.json" delegate:self];

to MyAppDelegate.m in the

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

I also added a method to MyAppDelegate.m

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    if ([request isGET]) {        
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
 }

so far so good everything is working and I see the results from my Rails app in the output!!!

As those things don't belong into MyAppDelegate.m I'm moving that stuff into my models. In my Titles.h I added

@interface Titles : NSManagedObject <RKRequestDelegate> {

and in Titles.m I added

+ (void) update {   
    [[RKClient sharedClient] get:@"/titles.json" delegate:self];
}

and

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    if ([request isGET]) {
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
}

In my MyAppDelegate.m I replaced :

 RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
   [client get:@"/titles.json" delegate:self];

with

  RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
  [Titles update];

when I run now I don't get any output.
I put several breakpoints, one in the - (void)didFinishLoad:(RKResponse*)response in the RKRequest file
and there the if test for :

if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
        [_delegate request:self didLoadResponse:finalResponse];
    }

fails while it succeeds in my first attempt (when everything is in MyAppDelegate)

I checked the variable _delate in de debugger and it says: _delegate = MyAppDelegate in my first attempt and _delegate = Titles in my second attempt (both like it should)

Why does that respondsToSelector fail ? (the delegate is correct and the method exists in Titles)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

薯片软お妹 2024-12-10 16:41:53

您的问题是您尝试将一个类设置为委托:

+ (void) update {   
  [[RKClient sharedClient] get:@"/titles.json" delegate:self];
}

self 这里是class Titles

回调(如预期)是一个实例方法:

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
  if ([request isGET]) {
    NSLog (@"Retrieved : %@", [response bodyAsString]);
  }
}

您应该有某种“DataModel”模型类(也许是“SongList”或任何有意义的类)。这通常是单例,因此您有一个 +sharedModel 实例。该实例是 RKClient 的委托。

Your problem is that your trying to set a class as the delegate:

+ (void) update {   
  [[RKClient sharedClient] get:@"/titles.json" delegate:self];
}

self here is the class Titles.

The callback is (as expected), an instance method:

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
  if ([request isGET]) {
    NSLog (@"Retrieved : %@", [response bodyAsString]);
  }
}

You should have some kind of "DataModel" model class (perhaps "SongList" or whatever makes sense). This is often a singleton, so you have a +sharedModel instance. That instance is what is the delegate for RKClient.

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