Skip to content

Instantly share code, notes, and snippets.

@billwang1990
Created October 14, 2015 07:44
Show Gist options
  • Save billwang1990/5506cd3f8e94043927a9 to your computer and use it in GitHub Desktop.
Save billwang1990/5506cd3f8e94043927a9 to your computer and use it in GitHub Desktop.
Non retain cycle NSTimer in Swift
public typealias TimerExcuteClosure = @convention(block)()->()
extension NSTimer{
public class func YQ_scheduledTimerWithTimeInterval(ti:NSTimeInterval, closure:TimerExcuteClosure, repeats yesOrNo: Bool) -> NSTimer{
return self.scheduledTimerWithTimeInterval(ti, target: self, selector: "excuteTimerClosure:", userInfo: unsafeBitCast(closure, AnyObject.self), repeats: true)
}
class func excuteTimerClosure(timer: NSTimer)
{
let closure = unsafeBitCast(timer.userInfo, TimerExcuteClosure.self)
closure()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment