我有一个 NSTimer
,我正在使用 scheduledTimerWithTimeInterval
方法,但我希望它作为参数的时间间隔是一个函数,而不是一个显式值。我怎么能这样做?
我的代码是这样的:
timer = NSTimer.scheduledTimerWithTimeInterval(time(), target: self, selector: "doStuff", userInfo: nil, repeats: true)
编译器提示第一个参数 time()
编辑:错误是“调用中的额外参数‘选择器’”
最佳答案
我不确定您为什么会收到那个 错误,但是time()
肯定不是您想要的时间间隔。这应该是定时器触发之间的秒数。对于像重复动画计时器这样的东西,通常是 1/30.0 或 1/60.0。
time
函数(顺便说一下,它应该有一个参数)返回一个整数,它是自“纪元”以来的秒数。在 Mac OS X 上,纪元是 2001 年 1 月 1 日世界标准时间凌晨 12:00。
编辑:从您的评论看来您已经定义了自己的time
函数。首先,我会将函数的名称更改为(比如)timeInterval
,这样它就不可能与系统的 time
函数发生冲突。我会确保您的函数返回 NSTimeInterval
,而不是 Float
或其他一些数字类型,例如:
func timeInterval() -> (NSTimeInterval) {
return 1/60.0
}
var timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval(), target: self, selector: "doStuff", userInfo: nil, repeats: true)
关于ios - NSTimer 'time' 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687282/