add timer into closure by phamquochoan · Pull Request #8 · radex/SwiftyTimer · GitHub
Skip to content
This repository was archived by the owner on May 1, 2026. It is now read-only.

add timer into closure - #8

Closed
phamquochoan wants to merge 1 commit into
radex:masterfrom
phamquochoan:master
Closed

phamquochoan wants to merge 1 commit into
radex:masterfrom
phamquochoan:master

Conversation

@phamquochoan

Copy link
Copy Markdown

So people can call invalidate() inside closure

@radex

radex commented Jun 24, 2015

Copy link
Copy Markdown
Owner

@phamquochoan

Copy link
Copy Markdown
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

radex commented Jun 24, 2015

Copy link
Copy Markdown
Owner

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
Copy Markdown
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

radex commented Jun 24, 2015

Copy link
Copy Markdown
Owner

Thanks, hopefully Swift can handle this case :)

@phamquochoan

Copy link
Copy Markdown
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

radex commented Apr 10, 2016

Copy link
Copy Markdown
Owner

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants