Created
January 4, 2017 09:15
-
-
Save jhonny-me/f729dd3369342eaed741a406e78f9912 to your computer and use it in GitHub Desktop.
Timer extension which breaks the retain cycle in Swift3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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