简体   繁体   中英

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. I'm just starting swift and Xcode, so I'm very new to this. 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. Your argument is an Int.

So replace :Int with :Double and it should work. You'll probably have to convert value to a Double as well. Swift doesn't like implicit conversions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM