views:

3954

answers:

4

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

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?

A: 

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.

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

Tony

Tony Lambert
The method signature should be -(void)onTimer:(NSTimer *)someTimer
lostInTransit
+3  A: 

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.

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.

Joel Levin
A: 

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

-(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

[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.

lostInTransit
+1  A: 

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

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

The selecter you pass to scheduledTimerWithTimeInterval must be:

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

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

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