I'm trying to use an NSTimer to run a function at a certain interval, but every time it runs the program crashes with an exception:
+[Project.Class updateLabel]: unrecognized selector sent to class...
Terminating app due to uncaught exception "NSInvalidArgumentException".
The code is as follows:
class SecondViewController: UIViewController {
// MARK: Properties
override func viewDidLoad() {
super.viewDidLoad()
do {
try updateLabel()
try startTimer()
}
catch{
self.showAlert()
}
}
@IBOutlet weak var i2cLabel: UILabel!
// MARK: Actions
func startTimer() throws {
_ = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateLabel"), userInfo: nil, repeats: true)
}
func showAlert() {
}
func updateLabel() throws {
}
}
If I comment out the NSTimer line, the program compiles and runs just fine.
Note that updateLabel(), the selector function, takes no arguments so calling it as a selector without a colon suffix should work. This seems to be unique to this problem.