I want to run a block of code in 10 seconds from an event, but I want to be able to cancel it so that if something happens before those 10 seconds, the code won\'t run after
Swift 3 has DispatchWorkItem:
DispatchWorkItem
let task = DispatchWorkItem { print("do something") } // execute task in 2 seconds DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: task) // optional: cancel task task.cancel()