[英]Swift, XCode: Is there a way to use a variable as a NSTimer.scheduledTimerWithTimeInterval Int?
I have a function, I'm trying to make it so every time it's called, it will invalidate an interval timer and make the interval faster. 我有一个函数,我正在尝试制作它,因此每次调用它都会使间隔计时器无效并使间隔更快。 When I try to put an Int variable in the code for NSTimer.scheduledTimerWithTimeInterval, Xcode comes up with an error.
当我尝试在NSTimer.scheduledTimerWithTimeInterval的代码中放入Int变量时,Xcode出现错误。 I'm just starting swift and Xcode, so I'm very new to this.
我刚刚开始使用swift和Xcode,所以我对此很陌生。 Thanks for the help.
谢谢您的帮助。
All the variables are set before the function is active. 在功能激活之前,所有变量均已设置。
func makeIntervalFaster() {
timer.invalidate()
value++
var timerValue:Int = 1/value
timer = NSTimer.scheduledTimerWithTimeInterval(timerValue, target: self, selector: Selector("action"), userInfo: nil, repeats: true)
}
The interval should be a NSTimeInterval which is an alias for Double. 该间隔应为NSTimeInterval,它是Double的别名。 Your argument is an Int.
您的论点是整数。
So replace :Int
with :Double
and it should work. 所以用
:Double
替换:Int
,它应该可以工作。 You'll probably have to convert value
to a Double as well. 您可能还必须将
value
转换为Double。 Swift doesn't like implicit conversions. Swift不喜欢隐式转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.