NSTimer timeInterval参数不允许变量

时间:2014-11-23 11:03:55

标签: xcode swift nstimer

当我使用

timer = NSTimer.scheduledTimerWithTimeInterval(0.75, target: self, selector: Selector("drawInTime"), userInfo: nil, repeats: false)

它工作正常,但如果我想为timeInterval参数使用变量(而不是0.75)

var waitTime = CGFloat(numberOMiliseconds) / 1000.0
timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval: waitTime, target: self, selector: Selector("drawInTime"), userInfo: nil, repeats: false)

timer = NSTimer.scheduledTimerWithTimeInterval(waitTime, target: self, selector: Selector("drawInTime"), userInfo: nil, repeats: false)

然后我得到以下错误“Swift编译器错误:调用中的额外参数'选择器'。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

如果你看一下scheduledTimerWithTimeInterval签名:

class func scheduledTimerWithTimeInterval(ti: NSTimeInterval, target aTarget: AnyObject, selector aSelector: Selector, userInfo: AnyObject?, repeats yesOrNo: Bool) -> NSTimer

您注意到该方法没有第一个参数的外部名称(方法的默认值),因此您不必使用timeInterval外部名称

此外,其预期类型为NSTimeInterval,这是Double的类型,但您传递的是CGFloat

因此,您应该将代码更改为:

var waitTime = Double(numberOMiliseconds) / 1000.0
timer = NSTimer.scheduledTimerWithTimeInterval(waitTime, target: self, selector: Selector("drawInTime"), userInfo: nil, repeats: false)