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

时间:2023-01-04 20:27:44

HI, I am developing a iphone app by using cocos2d. Its shown this msg.

嗨,我正在使用cocos2d开发一个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'  

here onTimer is a count down timer method. what is the solution for it?

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

4 个解决方案

#1


4  

Your onTimer method is being sent to an instance of NSArray for some reason. It is likely that you are either accidentally sending it to a real instance of NSArray, or that the object you are really trying to send it to has been released (aka, is no longer accessible) by the time the timer actually fires.

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

I would try to do some memory debugging to figure out if your timer target is being released at an inappropriate time. If everything looks ok, verify that you are indeed setting the timer target to the correct object.

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

#2


3  

The unrecognized selector error is most likely because you are passing the wrong text for the @selector parameter. Selector names MUST include ':' attributes whenever there is a parameter in the signature. So, if you have a timer method

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

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

The selecter you pass to scheduledTimerWithTimeInterval must be:

传递给scheduledTimerWithTimeInterval的选择器必须是:

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

The full call to NSTimer, would then look something like:

完全致电NSTimer,看起来像是这样的:

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

#3


0  

sounds like you are not providing a valid method to the timer to call on completion of the count down. You need to set both the method selector and the target to valid objects. see example below:

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

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

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

Maybe the target is being released before it returns?

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

Also you could try adding the following break points which will trap when the exception happens.

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

objc_exception_throw and -[NSException raise]. On the iPhone I think all exceptions travel through objc_exception_throw but if you're targetting Mac OS X Tiger or earlier you should set a breakpoint on both.

objc_exception_throw和 - [NSException raise]。在iPhone上我认为所有异常都是通过objc_exception_throw进行的,但是如果你的目标是Mac OS X Tiger或者更早,你应该在两者上设置一个断点。

There are more debugging techniques at http://www.cocoadev.com/index.pl?DebuggingTechniques.

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

Tony

#4


0  

Why is the onTimer method being called on an NSArray object? From your description, I believe onTimer has this definition

为什么在NSArray对象上调用onTimer方法?根据您的描述,我相信onTimer有这个定义

-(void)onTimer:(NSTimer *)aTimer

In that case, onTimer is a method of your viewcontroller (or another class you've created) but not a method of an array. How are you invoking the timer? The correct way to start a timer which will call this method is this

在这种情况下,onTimer是viewcontroller(或您创建的另一个类)的方法,但不是数组的方法。你是怎么调用计时器的?启动定时器的正确方法是调用此方法

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

The reason why this error occurs is either you are not invoking the timer properly or you are using some object which has been deallocated.

发生此错误的原因是您没有正确调用计时器,或者您正在使用已取消分配的某个对象。

#1


4  

Your onTimer method is being sent to an instance of NSArray for some reason. It is likely that you are either accidentally sending it to a real instance of NSArray, or that the object you are really trying to send it to has been released (aka, is no longer accessible) by the time the timer actually fires.

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

I would try to do some memory debugging to figure out if your timer target is being released at an inappropriate time. If everything looks ok, verify that you are indeed setting the timer target to the correct object.

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

#2


3  

The unrecognized selector error is most likely because you are passing the wrong text for the @selector parameter. Selector names MUST include ':' attributes whenever there is a parameter in the signature. So, if you have a timer method

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

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

The selecter you pass to scheduledTimerWithTimeInterval must be:

传递给scheduledTimerWithTimeInterval的选择器必须是:

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

The full call to NSTimer, would then look something like:

完全致电NSTimer,看起来像是这样的:

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

#3


0  

sounds like you are not providing a valid method to the timer to call on completion of the count down. You need to set both the method selector and the target to valid objects. see example below:

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

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

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

Maybe the target is being released before it returns?

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

Also you could try adding the following break points which will trap when the exception happens.

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

objc_exception_throw and -[NSException raise]. On the iPhone I think all exceptions travel through objc_exception_throw but if you're targetting Mac OS X Tiger or earlier you should set a breakpoint on both.

objc_exception_throw和 - [NSException raise]。在iPhone上我认为所有异常都是通过objc_exception_throw进行的,但是如果你的目标是Mac OS X Tiger或者更早,你应该在两者上设置一个断点。

There are more debugging techniques at http://www.cocoadev.com/index.pl?DebuggingTechniques.

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

Tony

#4


0  

Why is the onTimer method being called on an NSArray object? From your description, I believe onTimer has this definition

为什么在NSArray对象上调用onTimer方法?根据您的描述,我相信onTimer有这个定义

-(void)onTimer:(NSTimer *)aTimer

In that case, onTimer is a method of your viewcontroller (or another class you've created) but not a method of an array. How are you invoking the timer? The correct way to start a timer which will call this method is this

在这种情况下,onTimer是viewcontroller(或您创建的另一个类)的方法,但不是数组的方法。你是怎么调用计时器的?启动定时器的正确方法是调用此方法

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

The reason why this error occurs is either you are not invoking the timer properly or you are using some object which has been deallocated.

发生此错误的原因是您没有正确调用计时器,或者您正在使用已取消分配的某个对象。