【发布时间】:2016-07-05 02:45:58
【问题描述】:
我想安排将来的函数调用。我正在使用 Swift。
我想回调一个私有方法并返回一个 Promise(来自 PromiseKit)
我见过的所有例子都使用
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval, target: AnyObject, selector: Selector, userInfo: AnyObject?, repeats: Bool)
很好。我试过了
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "connect", userInfo: nil, repeats: false)
No method declared with Objective-C selector 'connect' 失败。
Objective-C 在这里做什么?
无论如何建议我在我的方法connect 前面添加@objc。美好的。好吧,我不能,因为显然Method cannot be marked @objc because its result type cannot be represented in Objective-C
如果我想使用 Objective-C,我就不会写 Swift...
还有一个scheduledTimerWithTimeInterval是
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval, invocation: NSInvocation, repeats: Bool)
但从我读到的 NSInvocation 不是 Swift 的东西......
所以我最终创建了一个包装器,它除了调用 connect 并返回 Objective C 可以理解的 Void 之外什么都不做。它有效,但感觉非常愚蠢。有没有更好的 Swift 方式?
奖励:为什么 javascript 可以像 setTimeout(this.connect, 1) 那样简单地做到这一点,而 Swift 没有我能找到的内置方式?
【问题讨论】: