简体   繁体   中英

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

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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