如何摆脱警告<class>可能无法响应<selector>?</selector> </class>

时间:2009-09-24 20:50:48

标签: objective-c iphone cocoa cocoa-touch

以下代码的行为与预期的一样,但编译器一直告诉我这个: 警告:'StatusViewController'可能无法响应'-beginLoadingThreadData'

如何摆脱这种警告,最重要的是为什么xcode认为是这种情况?

这是我的代码:

[self beginLoadingThreadData]; // called in the loadDidView block

- (void)beginLoadingThreadData
{
    [NSThread detachNewThreadSelector:@selector(loadThreadData) toTarget:self withObject:nil];
}

- (void)loadThreadData
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [NSThread sleepForTimeInterval:2];
    [self performSelectorOnMainThread:@selector(finishedLoadingThreadData) withObject:nil waitUntilDone:NO];
    [pool release];
}

- (void)finishedLoadingThreadData
{
    [[BusinessLogic instance] startTracking:self];
}

2 个答案:

答案 0 :(得分:7)

在使用之前声明该方法。我假设它是一个私有方法,在这种情况下,你会在实现文件的顶部添加一个类扩展:

@interface MyClass ()
- (void) beginLoadingThreadData;
@end

如果它是一个公共方法,请确保您已在标题中声明了该方法,#import编辑了实现文件顶部的标题。

答案 1 :(得分:1)

您必须在loadingThreadData的定义之前移动viewDidLoad的定义,或者在StatusViewController的标头文件中定义它,例如通过先插入-(void)beginLoadingThreadData;头文件中的@end(可能是StatusViewController.h)。

原因是如果你不这样做,当编译器到达你的viewDidLoad方法,并看到对beginLoadingThreadData的调用时,它还没有看到{{1}的定义并且(通过在头文件中看到带有方法签名的声明)并不确定这样的定义是否存在。因此,它会警告您,您尝试调用的方法可能不存在。