Extra argument 'selector' in call - NSTimer scheduledTimerWithTimeInterval

久未见 提交于 2019-12-20 02:37:09

问题


I have the following line of code:

changeColour = NSTimer.scheduledTimerWithTimeInterval(TIMES, target: self, selector: "changeColourOfPage", repeats: true)

but it gives the error "Extra argument 'selector' in call"

when I change the TIMES variable to a number like 1.0, it works fine. The variable TIMES is set to 1.0.

Is this just a glitch, or am I being stupid about something? I need to use it to run a method at random intervals.

Please help!


回答1:


It looks like you're missing the userInfo argument. Try this:

Swift 2

let TIMES = 1.0
var changeColour = NSTimer.scheduledTimerWithTimeInterval(TIMES, target: self, selector: "restart", userInfo: nil, repeats: true)

Swift 3, 4, 5

let TIMES = 1.0
var changeColour = Timer.scheduledTimer(timeInterval: TIMES, target: self, selector: #selector(restart), userInfo: nil, repeats: true)



回答2:


Just had this same issue. For me the problem was that I was passing a time interval as a Float not a Double. Simple fix was (using the code from the original post):

NSTimer.scheduledTimerWithTimeInterval(Double(TIMES), target: self, selector: Selector("changeColourOfPage"), userInfo:nil, repeats: true)

I had the same error message and once I cast the time as a Double it worked fine. Hope that helps someone!



来源:https://stackoverflow.com/questions/24723120/extra-argument-selector-in-call-nstimer-scheduledtimerwithtimeinterval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!