Swift 3
App Goal <iOS 10 :
Custom WeakTimer ( GitHubGist ):
final class WeakTimer { fileprivate weak var timer: Timer? fileprivate weak var target: AnyObject? fileprivate let action: (Timer) -> Void fileprivate init(timeInterval: TimeInterval, target: AnyObject, repeats: Bool, action: @escaping (Timer) -> Void) { self.target = target self.action = action self.timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector:
Using:
let timer = WeakTimer.scheduledTimer(timeInterval: 2, target: self, repeats: true) { [weak self] timer in
timer is an instance of the standard timer class, so you can use all available methods (e.g. invalidate , fire , isValid , fireDate , etc.).
The timer instance will be freed if self freed or a timer job is executed (for example, repeats == false ).
App Goal> = iOS 10 :
Standard timer implementation:
open class func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Swift.Void) -> Timer
Using:
let timer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { [weak self] timer in
Vlad Papko Dec 6 '16 at 20:11 2016-12-06 20:11
source share