swifttimerunrecognized-selector

Terminating app due to uncaught exception 'NSInvalidArgumentException'. unrecognized selector sent to instance


I have timerRunner() function that runs when button is clicked:

func timerRunner() {
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: target.self, selector: (#selector(self.timeUpdater)), userInfo: nil, repeats: true)
    }

Selector of Timer.scheduledTimer is selected as following:

@objc func timeUpdater() {
    counter = counter + 1

    timerLabel.text = secondConverter(seconds: counter)

}

But this is an error message I get:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue timeUpdater]: unrecognized selector sent to instance '


Solution

  • As far as I know, timeUpdater function should have one argument of type Timer. Something like that:

    @objc func timeUpdater(_ timer: Timer) {
        counter = counter + 1
        timerLabel.text = secondConverter(seconds: counter)
    }