什么是swift 3中的NSTimer.scheduledTimerWithTimeInterval()的替换?

时间:2016-09-20 08:28:41

标签: nstimer swift3 xcode8

我想在Swift 3中实现NSTimer.scheduledTimerWithTimeInterval。我想知道替换

NSTimer.scheduledTimerWithTimeInterval(0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)

正如我们在swift 2.2中使用它

2 个答案:

答案 0 :(得分:39)

试试这样:

Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)

答案 1 :(得分:-1)

如果您想根据您的要求无效,您也可以使用Timer实例。

//Create Timer instance
var objTimer = Timer()

//Schedule Timer
self.objTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerTriggered), userInfo: nil, repeats: true)

//Invalidate Timer after task complete
self.objTimer.invalidate()