【问题标题】:IPhone app terminating due to uncaught exception由于未捕获的异常,iPhone 应用程序终止
【发布时间】:2009-01-26 10:29:55
【问题描述】:

嗨, 我正在使用 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'  

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

【问题讨论】:

    标签: iphone objective-c cocos2d-iphone


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      @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];
      

      【讨论】:

        【解决方案3】:

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

        [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 有更多调试技术。

        托尼

        【讨论】:

        • 方法签名应该是-(void)onTimer:(NSTimer *)someTimer
        【解决方案4】:

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

        -(void)onTimer:(NSTimer *)aTimer
        

        在这种情况下,onTimer 是您的视图控制器(或您创建的另一个类)的方法,而不是数组的方法。你是如何调用定时器的?启动将调用此方法的计时器的正确方法是这样

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

        出现此错误的原因是您没有正确调用计时器,或者您正在使用一些已被释放的对象。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-20
          • 2023-03-11
          • 2018-04-23
          • 2014-11-03
          • 2014-01-05
          相关资源
          最近更新 更多