Last active
April 1, 2016 17:35
-
-
Save dimpiax/e2adc74c25d76d124fc3 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
// | |
// Extensions.swift | |
// Tests | |
// | |
// Created by Pilipenko Dima on 4/1/15. | |
// Copyright (c) 2015 Pilipenko Dima. All rights reserved. | |
// | |
import Foundation | |
extension NSTimer { | |
class func delay(value: NSTimeInterval, completion: () -> Void) { | |
NSTimer.scheduledTimerWithTimeInterval(value, target: self, selector: "timerDidFired:", userInfo: ["box": Box(completion)], repeats: false) | |
} | |
class func timerDidFired(timer: NSTimer) { | |
if let userInfo = timer.userInfo as? [String: Box], box = userInfo["box"] { | |
let callback = box.unbox() | |
callback() | |
} | |
} | |
} | |
private class Box { | |
typealias Callback = () -> Void | |
private var _callback: Callback! | |
init(_ callback: Callback) { | |
self._callback = callback | |
} | |
func unbox() -> Callback { | |
return _callback | |
} | |
deinit { | |
_callback = nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: