【问题标题】:Uncaught Exception while building a timer构建计时器时未捕获的异常
【发布时间】:2016-02-11 10:20:37
【问题描述】:

由于未捕获的异常,我不断收到 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)

}

谢谢!

【问题讨论】:

  • Selector("increaseTime") vs Selector("increaseTimer") 因为你的方法被命名为func increaseTimer() {}

标签: ios uncaught-exception


【解决方案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)

}

【讨论】:

  • 太棒了,非常感谢!
【解决方案2】:

崩溃消息中最重要的部分是

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

您的计时器发送了一个选择器increaseTime,但没有同名的属性或方法。

编程是一项非常精确的业务。缺少字符或大小写不匹配可能会破坏整个系统。

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

【讨论】:

    【解决方案3】:

    这是我在我的一个项目中使用的,它运行良好。

    这是计时器:

    self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, 目标: self, 选择器:“startQuestionTimer:”,用户信息:无,重复:真)

    方法如下:

      func startQuestionTimer(obj:NSTimer){
      }
    

    这应该是肯定的。

    【讨论】:

    • 传递的参数不是强制性的,但在NSTimer的情况下可能会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多