-
-
Save smilingmiao/11de50505dc7c3c54d69a2edab5f95ed to your computer and use it in GitHub Desktop.
NSTimer extension which breaks the retain cycle in Swift.
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
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