Having trouble with NSTimer (Swift)

2019-04-05 08:24发布

--EDITED WITH UPDATED INFORMATION--

What I wish to do is call a function named timerFunc once every five seconds using a NSTimer.scheduledTimerWithTimeInterval method, the issue seems is that during runtime, I get the error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-      
[Animation.ViewController timerFunc:]: unrecognized selector sent to instance 0x7fe548d66040'

In the output log. I've been looking up other people's NSTimers to no avail, I see quite a few have the selector as selector: Selector("timerFunc:") instead of selector: Selector("timerFunc") both ways, however, give the error. Another thing is that both the timerFunc function and the NSTimer are inside of viewDidLoad, are there any issues with that? Any insight on the problem is greatly appreciated, thanks for reading.

timerFunc below

func timerFunc(){

    println("Timer")
}

NSTimer below

NSTimer.scheduledTimerWithTimeInterval(
        5.0,
        target: self,
        selector: Selector("timerFunc"),
        userInfo: nil,
        repeats: true)

标签: swift nstimer
5条回答
手持菜刀,她持情操
2楼-- · 2019-04-05 08:27

Another thing is that both the timerFunc function and the NSTimer are inside of viewDidLoad, are there any issues with that?

Yes. That is your problem. The timerFunc can't be nested inside of viewDidLoad, it must be a top level function at the same level as viewDidLoad.

class ViewController: UIViewController {

    override func viewDidLoad() {
        ....
        NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "timerFunc", userInfo: nil, repeats: true)
    }

    func timerFunc() {
        println("Timer")
    }

}

When the timer fires, it will call the function specified by the selector on the the object designated by target. That is, it will call self.timerFunc(). When your timerFunc() is nested inside of ViewDidLoad, it can't find it.

查看更多
SAY GOODBYE
3楼-- · 2019-04-05 08:33

When using a selector in Swift you need to use the Selector() pseudo-function around the name of the function:

NSTimer.scheduledTimerWithTimeInterval(5.0,   target: self, 
                                            selector: Selector("timerFunc"), 
                                            userInfo: nil,
                                             repeats: true);

The reason for the seemingly confusing errors I suspect is due to the compilers inability to match your argument list to a particular method, due to the selector name being of the wrong type (string rather than selector).

This also explains the fact that when you did manage to get it to compile (as mentioned in the comments to another answer) it failed at runtime. The type checking at compile time fails to pick up this sort of error (hence the runtime exception reporting the 'unrecognized selector').

查看更多
迷人小祖宗
4楼-- · 2019-04-05 08:44

The first parameter of a Swift method does not include a label. All others generally do. You should expect the syntax to be:

NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "timerFunc", userInfo: nil, repeats: true)
查看更多
老娘就宠你
5楼-- · 2019-04-05 08:47

My problem with this was the selector was pointing at a private function.

查看更多
聊天终结者
6楼-- · 2019-04-05 08:48

Timers will not work with private method callbacks. Also, make sure your class inherits from NSObject. Pure Swift classes will not work.

More information can be found here: https://github.com/NxSoftware/NxSwiftTimer

查看更多
登录 后发表回答