构建计时器时未捕获异常

时间:2016-02-11 10:20:37

标签: ios uncaught-exception

由于未捕获的异常,我一直收到SIGABRT错误:

  2016-02-11 01:54:23.232 time_01[1937:52731] -[time_01.ViewController increaseTime]: unrecognized selector sent to instance 0x7fdf72d12c20
2016-02-11 01:54:23.235 time_01[1937:52731] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[time_01.ViewController increaseTime]: unrecognized selector sent to instance 0x7fdf72d12c20'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e6adf45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001103d1deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e6b656d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010e603eea ___forwarding___ + 970
    4   CoreFoundation                      0x000000010e603a98 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x000000010ea93181 __NSFireTimer + 83
    6   CoreFoundation                      0x000000010e60e264 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x000000010e60de11 __CFRunLoopDoTimer + 1089
    8   CoreFoundation                      0x000000010e5cf821 __CFRunLoopRun + 1937
    9   CoreFoundation                      0x000000010e5cee08 CFRunLoopRunSpecific + 488
    10  GraphicsServices                    0x0000000112ca1ad2 GSEventRunModal + 161
    11  UIKit                               0x000000010eeca30d UIApplicationMain + 171
    12  time_01                             0x000000010e4d140d main + 109
    13  libdyld.dylib                       0x0000000110ed992d start + 1
    14  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

我查看了我的代码中的简单计时器,我无法弄清楚如何更改代码,因此它不会返回错误。

var timer = NSTimer()

var time = 0

@IBOutlet var timeText: UILabel!

func increaseTimer() {

        time = time + 1

        timeText.text = "\(time)"

}

@IBAction func play(sender: AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("increaseTime"), userInfo: nil, repeats: true)

}

谢谢!

3 个答案:

答案 0 :(得分:1)

这是因为您的函数名称为func increaseTimer(),而在设置选择器时,您正在设置selector: Selector("increaseTime")

因此,要解决您的问题,请重命名您的方法:

func increaseTime() {

        time = time + 1

        timeText.text = "\(time)"

}

@IBAction func play(sender: AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("increaseTime"), userInfo: nil, repeats: true)

}

或者在设置选择器时更新方法名称:

func increaseTimer() {

        time = time + 1

        timeText.text = "\(time)"

}

@IBAction func play(sender: AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("increaseTimer"), userInfo: nil, repeats: true)

}

答案 1 :(得分:0)

崩溃消息最重要的部分是

  

由于未捕获的异常而终止应用   'NSInvalidArgumentException',原因:' - [time_01.ViewController   increaseTime]:无法识别的选择器发送到实例0x7fdf72d12c20'

您的计时器会发送一个选择器increaseTime,但没有具有此名称的属性或方法。

编程是一项非常精确的业务。丢失的字符或案例不匹配可能会破坏整个系统。

您必须确保方法和选择器的签名完全匹配。

答案 2 :(得分:0)

这是我在我的一个项目中使用的,它工作正常。

这是计时器:

  

self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0,target:self,   selector:“startQuestionTimer:”,userInfo:nil,重复:true)

以下是方法:

  func startQuestionTimer(obj:NSTimer){
  }

这应该可以肯定。