ios - NSTimer 'time' 问题

标签 ios swift nstimer

我有一个 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/

相关文章:

Swift:使用 Array<Character> 或 Slice<Character> 类型参数的 Join 方法在 Release 配置中崩溃

iphone - 使用 UIViewAnimation 调用时 NSTimer 卡住

swift - 如何在 View Controller 中使用 UILabel 中显示的计时器生成的数字作为整数?

iphone - RMMapView setCenterCoordinate 函数不正常

ios - 如何在 Objective C (iOS) 中将字符串转换为对象变量

ios - 扩展隐藏了我想要访问的属性。解决方法?

swift - 在 swift 中,为什么我不能实例化一个有初始化器的协议(protocol)?

ios - UIView 不是第一次消除

swift - 从 Firebase 数据库获取数据?

iphone - 即使在后台,如何让 AVAudioPlayer 在 30 分钟后停止播放背景音乐?