由于未捕获异常而终止的iPhone应用程序

问题描述:

嗨, 我正在使用cocos2d开发iphone应用程序。它显示了这个味精。由于未捕获异常而终止的iPhone应用程序

2009-01-26 16:17:40.603 Find The Nuts[449:20b] *** -[NSCFArray onTimer:]: unrecognized selector sent to instance 0x59be030 
2009-01-26 16:17:40.605 Find The Nuts[449:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFArray onTimer:]: unrecognized selector sent to instance 0x59be030' 

这里onTimer是一个倒数计时器的方法。它的解决方案是什么?

听起来像你没有提供一个有效的方法来定时器调用完成倒计时。您需要将方法选择器和目标都设置为有效的对象。看到下面的例子:

[NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 

- (void)onTimer { 
    NSLog(@"hello!"]; 
} 

也许目标被释放之前,它返回?

此外,您可以尝试添加以下断点,这些断点将在发生异常时进行陷阱。

objc_exception_throw和 - [NSException raise]。在iPhone上,我认为所有例外都是通过objc_exception_throw传播的,但如果您的目标是Mac OS X Tiger或更早的版本,则应在两者上设置断点。

http://www.cocoadev.com/index.pl?DebuggingTechniques有更多调试技术。

Tony

+0

的方法签名应该是 - (void)onTimer:(NSTimer *)someTimer – lostInTransit 2009-01-27 15:24:29

由于某种原因,您的onTimer方法正在发送到NSArray的实例。很可能你不小心将它发送给一个真正的NSArray实例,或者你实际尝试发送它的对象已经在计时器实际触发时被释放(也就是不再可访问)。

我会尝试做一些内存调试,以确定您的计时器目标是否在不适当的时间发布。如果一切正常,请确认您确实将计时器目标设置为正确的对象。

为什么在一个NSArray对象上调用onTimer方法?从你的描述,我相信的OnTimer有这个定义

-(void)onTimer:(NSTimer *)aTimer 

在这种情况下,的OnTimer是你的视图 - 控制的方法(或者你已经创建了另一个类),而不是数组的方法。你怎么调用定时器?启动一个计时器,将调用此方法正确的方法是这样的

[NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]; 

为什么会出现此错误是要么你没有正确地调用定时器或正在使用的已释放某个对象的原因。

无法识别的选择器错误很可能是因为您为@selector参数传递了错误的文本。只要签名中有参数,选择器名称就必须包含':'属性。所以,如果你有一个计时器方法

-(void) onTimer:(NSTimer*)timer { ... } 

传递给scheduledTimerWithTimeInterval的selecter必须是:

@selector(onTimer:) // note the ':' at the end of the name! 

完整调用的NSTimer,那么看起来像:

[NSTimer scheduledTimerWithTimeInterval:1 
           target:self 
           selector:@selector(OnTimer:) // note the ':' 
           userInfo:nil 
           repeats:NO];