Skip to content

Instantly share code, notes, and snippets.

@IainDelaney
Created July 31, 2015 14:24
Show Gist options
  • Save IainDelaney/9f59050b2ac4a97fc7cf to your computer and use it in GitHub Desktop.
Save IainDelaney/9f59050b2ac4a97fc7cf to your computer and use it in GitHub Desktop.
@objc protocol Timer {
func scheduleTimer(target:AnyObject, selector: Selector, interval: NSTimeInterval)->String
func cancelTimer(id:String)
}
@objc class NSTimerService: Timer {
var timers:[String:NSTimer] = [:]
func scheduleTimer(target:AnyObject, selector: Selector, interval: NSTimeInterval)->String {
let id = NSUUID().UUIDString
timers[id] = NSTimer.scheduledTimerWithTimeInterval(interval, target: target, selector: selector, userInfo: nil, repeats: true)
return id
}
func cancelTimer(id:String) {
timers[id]?.invalidate()
}
deinit {
println("Timer service destoryed.")
for t in timers.keys {
timers[t]?.invalidate()
}
timers.removeAll(keepCapacity: false)
}
}
@IainDelaney
Copy link
Author

How many stupids can you find in this code, not including the entire concept?

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