Skip to content

Instantly share code, notes, and snippets.

@dimpiax
Last active April 1, 2016 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimpiax/e2adc74c25d76d124fc3 to your computer and use it in GitHub Desktop.
Save dimpiax/e2adc74c25d76d124fc3 to your computer and use it in GitHub Desktop.
//
// 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
}
}
@dimpiax
Copy link
Author

dimpiax commented Apr 1, 2015

Usage:

NSTimer.delay(2) {
    println("Psh!")
}

@dimpiax
Copy link
Author

dimpiax commented Apr 1, 2016

Now you can check lib here: https://github.com/dimpiax/RichTimer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment