Skip to content

add timer into closure#8

Closed
phamquochoan wants to merge 1 commit intoradex:masterfrom
phamquochoan:master
Closed

add timer into closure#8
phamquochoan wants to merge 1 commit intoradex:masterfrom
phamquochoan:master

Conversation

@phamquochoan
Copy link

So people can call invalidate() inside closure

@radex
Copy link
Owner

radex commented Jun 24, 2015

But it you do that, then you'll have to write { timer in ... } when defining a closure, no?

@phamquochoan
Copy link
Author

Yes, we will have to write that, or { _ in ... }
It's mainly for the sake of NSTimer.every(#time#) { ... } because most of the time we don't have to create a var or let for it.
We can write invalidate condition inside the closure.

With NSTimer.after..., we can keep the old way.

@radex
Copy link
Owner

radex commented Jun 24, 2015

Maybe we can overload every with a second variant that doesn't take any arguments in its closure? Can you check if it's possible to make the timer argument optional in some way?

@phamquochoan
Copy link
Author

Theorically, yes. But I'm afraid of the error ambiguous use of ...
For example (this one from ExSwift):
each (call: (Element) -> ())
each (call: (Int, Element) -> ())

Sometimes I cannot use it likes each { item in ... } or each { $0.doSth() }
I have to write it this way: each { (index, item) in ... } oreach { (_, item) in ... } `
Swift thinks these methods are ambiguous, I don't know why though.

I'll give it a try.

@radex
Copy link
Owner

radex commented Jun 24, 2015

Thanks, hopefully Swift can handle this case :)

@phamquochoan
Copy link
Author

ambiguous use of 'after'...

Best I can do is:

NSTimer.after(5.0.seconds) { // it works
       log("abc")
}

NSTimer.after(5.0.seconds) { (timer: NSTimer!) -> Void in // this one works too
        log("abc")
}

NSTimer.after(5.0.seconds) { timer in // ambiguous
        log("abc")
}

I have to doubled every function in souce code file, and it looks ugly because Objective-C doesn't handle function overload as well as swift.

@radex radex mentioned this pull request Feb 29, 2016
@radex
Copy link
Owner

radex commented Apr 10, 2016

Done on master.

@radex radex closed this Apr 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants