Anton Zimin
Anton Zimin

Reputation: 101

Xcode 7.2 and NSTimer throws uncaught exception

Here is the code:

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

    }
}

I don't know why, but on my new computer this simple thing doesn't work.

Here is what it gives me as a error:

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) 

It always worked before on my old computer with old version of xCode

Upvotes: 1

Views: 102

Answers (1)

vadian
vadian

Reputation: 285170

Methods conforming to the target / selector pattern must be declared on the top level of the class.

class ViewController: UIViewController {

...

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

    let interval: Double = 2.0 // 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
  }

...

}

It certainly did not work on your old computer with the nested syntax.

Upvotes: 1

Related Questions