[英]Extra argument 'selector' in call error
class ViewController: UIViewController {
func ChangePage()
{
NSLog("Hej")
}
var timers = NSTimer(NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)
}
我从Xcode 6中收到以下错误:调用中的额外参数“选择器”
我已经尝试了几种配置,这与放置在代码中的位置有关吗?
您应该在构造函数中添加timeInterval,例如:
NSTimer(timeInterval: NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)
是的,这与放置位置无关紧要。 问题在于,计时器是一个属性,它是在初始化之前创建的。 因此,当创建它时,自身并不存在,但是您引用它,就会引起问题。
您可能要使用:
var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "changePage", userInfo: nil, repeats: true)
这将返回一个计时器,该计时器已添加到运行循环并自动触发。
要停止触发计时器,您必须像这样将其无效
timer.invalidate()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.