【发布时间】:2015-10-14 22:11:53
【问题描述】:
目前,我正在尝试将后台线程简化为应用程序中的主线程执行。
我这样做的方式是这样的:
import Foundation
infix operator ~> {}
private let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0)
func ~> (backgroundClosure: () -> (), mainClosure: () -> ()) {
dispatch_async(queue) {
backgroundClosure()
dispatch_async(dispatch_get_main_queue(), mainClosure)
}
}
这会让我做类似的事情:
{ println("executed in background thread") } ~> { println("executed in main thread") }
现在...我想将此功能扩展到可能能够 dispatch_after 到主线程,所以也许我希望它在 0.25 秒后被调用或什么的。
有没有办法通过某种方式传入参数来实现这一点?
理想情况下,我可以实现backgroundClosure ~>(0.25) mainClosure 之类的东西,但我怀疑这是可能的
【问题讨论】:
标签: swift operator-overloading infix-notation