Created
January 17, 2019 07:32
-
-
Save danielgarbien/cdd8b80e7764b6bfb12bbdacebfda64f to your computer and use it in GitHub Desktop.
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
@objcMembers | |
private class BlockHandler: NSObject { | |
var block: (() -> Void)? | |
@objc func runBlock() { | |
block?() | |
} | |
} | |
private extension Timer { | |
class func iOS9Ready_scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: @escaping (Timer) -> Swift.Void) -> Timer { | |
if #available(iOS 10.0, *) { | |
return scheduledTimer(withTimeInterval: interval, repeats: repeats, block: block) | |
} else { | |
let blockHandler = BlockHandler() | |
let timer = Timer.scheduledTimer(timeInterval: interval, target: blockHandler, selector: #selector(BlockHandler.runBlock), userInfo: nil, repeats: repeats) | |
blockHandler.block = { [weak timer] in | |
guard let timer = timer else { return } | |
block(timer) | |
} | |
return timer | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment