I want to schedule a function call in the future. I'm using Swift.
I want to callback a method that is private and returns a Promise (from PromiseKit)
All the example I've seen use
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval, target: AnyObject, selector: Selector, userInfo: AnyObject?, repeats: Bool)
Fine. I've tried
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "connect", userInfo: nil, repeats: false)
That fails with No method declared with Objective-C selector 'connect'
.
What is Objective-C doing here??
Anyway it's suggested that I add @objc
in front of my method connect
. Fine. Well I can't because apparently Method cannot be marked @objc because its result type cannot be represented in Objective-C
If I wanted to use objective-C I'd not be writing Swift...
There's another scheduledTimerWithTimeInterval
that is
NSTimer.scheduledTimerWithTimeInterval(ti: NSTimeInterval, invocation: NSInvocation, repeats: Bool)
But from what I've read NSInvocation
is not a Swift thing...
So I ended up creating a wrapper that does nothing other than calling connect
and returning Void
that Objective C can understand. It works but it feels very stupid. Is there a better Swift way?
Bonus: why can javascript do that as simply as setTimeout(this.connect, 1)
and Swift has no built in way I can find?