Xcode 7.2和NSTimer抛出未捕获的异常

时间:2016-02-07 13:53:10

标签: swift timer unrecognized-selector

以下是代码:

import UIKit

class ViewController: UIViewController {

    var clickNumber:Int = 0

    @IBOutlet weak var CountLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        CountLabel.text = "0"

        let interval: Double = 60/120*4

        var metronomeTimer = NSTimer.scheduledTimerWithTimeInterval(interval, target: self, selector: "metronomeFunc:", userInfo: nil, repeats: true)

        func metronomeFunc(timer:NSTimer) {
            CountLabel.text = String(clickNumber)
            clickNumber = clickNumber+1

        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
}

我不知道为什么,但在我的新电脑上,这个简单的事情并不起作用。

以下是它给我的错误:

2016-02-07 16:45:19.683 Move And Play[14743:2005023] -[Move_And_Play.ViewController metronomeFunc:]: unrecognized selector sent to instance 0x7f8c0a435c20
2016-02-07 16:45:19.695 Move And Play[14743:2005023] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Move_And_Play.ViewController metronomeFunc:]: unrecognized selector sent to instance 0x7f8c0a435c20'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010730ee65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010904edeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010731748d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010726490a ___forwarding___ + 970
    4   CoreFoundation                      0x00000001072644b8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001076f40d1 __NSFireTimer + 83
    6   CoreFoundation                      0x000000010726ec84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    7   CoreFoundation                      0x000000010726e831 __CFRunLoopDoTimer + 1089
    8   CoreFoundation                      0x0000000107230241 __CFRunLoopRun + 1937
    9   CoreFoundation                      0x000000010722f828 CFRunLoopRunSpecific + 488
    10  GraphicsServices                    0x000000010bba3ad2 GSEventRunModal + 161
    11  UIKit                               0x0000000107b2b610 UIApplicationMain + 171
    12  Move And Play                       0x0000000106e2c17d main + 109
    13  libdyld.dylib                       0x0000000109b8592d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

以前在我的旧计算机上使用旧版本的xCode

总是有效

1 个答案:

答案 0 :(得分:1)

必须在类的顶层声明符合目标/选择器模式的方法。

class Person: Object {
    var dynamic firstName: String = ""
    var dynamic lastName: String = ""
}

使用嵌套语法肯定无法在旧计算机上运行。