32

I have a problem with delaying computer's move in a game.

I've found some solutions but they don't work in my case, e.g.

var delay = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: nil, userInfo: nil, repeats: false)

I tried to use this with function fire but also to no effects.

What other possibilities there are?

3
  • 2
    You clearly don't understand what an NSTimer does. You need to have a selector, and have some code in that method you want to run when the timer fires. A timer without a selector doesn't do anything.
    – rdelmar
    Jan 16, 2015 at 18:11
  • This answer is what you're looking for stackoverflow.com/a/24318861/3810673
    – Ian
    Jan 16, 2015 at 18:16
  • thank you very much! I didn't understand the selector because I don't get it why it should be in apostrophe. I put there my func and eveything works great.
    – Dandy
    Jan 16, 2015 at 18:20

4 Answers 4

108

Swift 3

With GCD:

let delayInSeconds = 4.0
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delayInSeconds) {

    // here code perfomed with delay

}

or with a timer:

func myPerformeCode() {

   // here code to perform
}
let myTimer : Timer = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(self.myPerformeCode), userInfo: nil, repeats: false)

Swift 2

With GCD:

let seconds = 4.0
let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

dispatch_after(dispatchTime, dispatch_get_main_queue(), {

   // here code perfomed with delay

})

or with a timer:

func myPerformeCode(timer : NSTimer) {

   // here code to perform
}
let myTimer : NSTimer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("myPerformeCode:"), userInfo: nil, repeats: false)
2
  • that's a great solution without making any extra methods. I didn't think about using parameters to my main method, thank you!
    – Dandy
    Jan 16, 2015 at 19:20
  • Wanted to upvote, only to discover I already did in the past :) Feb 3, 2017 at 18:31
7

With Swift 4.2

With Timer You can avoid using a selector, using a closure instead:

    Timer.scheduledTimer(withTimeInterval: 1.0, repeats: false) { (nil) in
        // Your code here
    }

Keep in mind that Timer is toll-free bridged with CFRunLoopTimer, and that run loops and GCD are two completely different approaches.... e

2

In swift we can delay by using Dispatch_after.

SWift 3.0 :-

DispatchQueue.main.asyncAfter(deadline: .now()+4.0) {

        alert.dismiss(animated: true, completion: nil)
    }
0

How about using Grand Central Dispatch?

https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html

Valfer has shown you how

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.