Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smilingmiao/11de50505dc7c3c54d69a2edab5f95ed to your computer and use it in GitHub Desktop.
Save smilingmiao/11de50505dc7c3c54d69a2edab5f95ed to your computer and use it in GitHub Desktop.
NSTimer extension which breaks the retain cycle in Swift.
private class Block<T> {
let f : T
init (_ f: T) { self.f = f }
}
extension NSTimer {
static func xxx_scheduledTimerWithTimeInterval(ti: NSTimeInterval, block: ()->(), repeats: Bool) -> NSTimer {
return self.scheduledTimerWithTimeInterval(ti, target:
self, selector: "xxx_blcokInvoke:", userInfo: Block(block), repeats: repeats)
}
static func xxx_blcokInvoke(timer: NSTimer) {
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