在Swift中调度闭包表达式

cwh*_*ris 5 closures nstimer swift

我希望能够安排在未来的绝对或相对时间运行闭包.我看到我可以NSTimer用来安排稍后调用的选择器,但这不是我想要的.我更愿意看到这样的事情:

let timer = NSTimer.scheduleWithTimeInterval(
    ti: NSTimerInterval(1.0),
    action: {
        // do something
    }
)
Run Code Online (Sandbox Code Playgroud)

在Swift中有没有内置的方法来做这样的事情?

编辑

我现在遇到的dispatch_after似乎更符合我的想法,但我对其他想法持开放态度.

bjt*_*tus 4

dispatch_after应该是一个很好的解决方案,因为没有基于块的 NSTimer 方法。

或者,您可以使用(或创建)一个基于简单块的 NSTimer 类别(或 Swift 中的扩展):https://github.com/jivadevoe/NSTimer-Blocks