简体   繁体   English

调用错误中的额外参数“选择器”

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

} }

I get the following error from Xcode 6: Extra Argument 'selector' in call 我从Xcode 6中收到以下错误:调用中的额外参数“选择器”

I've tried several configurations, does it have something to do with where in the code it's placed? 我已经尝试了几种配置,这与放置在代码中的位置有关吗?

You should add timeInterval in the constructor like: 您应该在构造函数中添加timeInterval,例如:

NSTimer(timeInterval: NSTimeInterval(0.5), target:self, selector: "ChangePage", userInfo: nil, repeats: true)

And yes, it does matter where you put. 是的,这与放置位置无关紧要。 The problem is, that timers is a property, and it is created before the initialization. 问题在于,计时器是一个属性,它是在初始化之前创建的。 So when it is created, self is not existing, but you refer to it, and that causes the problem. 因此,当创建它时,自身并不存在,但是您引用它,就会引起问题。

You might want to use: 您可能要使用:

var timer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "changePage", userInfo: nil, repeats: true)

This returns a timer that is already added to the run loop and fires automatically. 这将返回一个计时器,该计时器已添加到运行循环并自动触发。

To stop the timer to fire, you must invalidate it like this 要停止触发计时器,您必须像这样将其无效

timer.invalidate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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