Skip to content

Instantly share code, notes, and snippets.

@jhonny-me
Created January 4, 2017 09:15
Show Gist options
  • Save jhonny-me/f729dd3369342eaed741a406e78f9912 to your computer and use it in GitHub Desktop.
Save jhonny-me/f729dd3369342eaed741a406e78f9912 to your computer and use it in GitHub Desktop.
Timer extension which breaks the retain cycle in Swift3
// coming form https://gist.github.com/onevcat/2d1ceff1c657591eebde, adapt swift3
private class Block<T> {
let f : T
init (_ f: T) { self.f = f }
}
extension Timer {
static func xxx_scheduledTimer(timeInterval ti: TimeInterval, repeats: Bool, block: ()->()) -> Timer {
return self.scheduledTimer(timeInterval: ti, target: self, selector: #selector(xxx_blcokInvoke), userInfo: Block(block), repeats: repeats)
}
static func xxx_blcokInvoke(timer: Timer) {
if let block = timer.userInfo as? Block<()->()> {
block.f()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment