简体   繁体   English

Swift,XCode:有没有办法将变量用作NSTimer.scheduledTimerWithTimeInterval Int?

[英]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.

相关问题 Swift的NSTimer.scheduledTimerWithTimeInterval的两个参数集 - Two parameter sets for Swift's NSTimer.scheduledTimerWithTimeInterval 如何停止NSTimer.scheduledTimerWithTimeInterval - How to stop NSTimer.scheduledTimerWithTimeInterval 当按下主页按钮时,如何在spritekit [SWIFT]中暂停和重新启动NSTimer.scheduledTimerWithTimeInterval? - How to pause and restart NSTimer.scheduledTimerWithTimeInterval in spritekit [SWIFT] when home button is pressed? 无法识别的选择器使用NSTimer.scheduledTimerWithTimeInterval函数发送到实例 - Unrecognized selector sent to instance with NSTimer.scheduledTimerWithTimeInterval function NSTimer ScheduledTimerWithTimeInterval-不调用功能 - NSTimer scheduledTimerWithTimeInterval - not calling funciton NSTimer错误(Swift:Xcode) - Error in NSTimer (Swift: Xcode) 自动锁定ios设备,NSTimer和scheduledTimerWithTimeInterval - autolocking ios device, NSTimer and scheduledTimerWithTimeInterval 如何完全删除NSTimer ScheduledTimerWithTimeInterval - How to remove NSTimer scheduledTimerWithTimeInterval entirely 我可以使用NSTimer的ScheduledTimerWithTimeInterval方法1天(24小时)是否重复? - Can I use NSTimer's scheduledTimerWithTimeInterval method for 1 day (24 hour) with or without repeats? 使用NSTimer,以相同的名称,不同的参数调用两个函数,都使用scheduleTimerWithTimeInterval - Using NSTimer, calling two functions with the same name, different parameters, both use scheduledTimerWithTimeInterval
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM