0

In earlier versions of Swift this code works perfectly.

DispatchQueue.main.asyncAfter(deadline: .now() + delay) { timer in self.setOriginalState() self.shakeAnimation() } 

But in Swift 4 the following error appears:

Ambiguous reference to member 'asyncAfter(deadline:qos:flags:execute:)'

How can one create a delay before running a sequence of code in Swift 4?

2
  • 1
    just remove "timer in".then work fine. Commented Sep 23, 2017 at 9:56
  • 1
    "In earlier versions of Swift this code works perfectly" Nevertheless, it was always wrong. All that happened is that the compiler stopped being so forgiving. Commented Sep 23, 2017 at 17:38

2 Answers 2

4

Trying in playground it doesn't give any issue, that is just "missing" is the "timer" reference.

DispatchQueue.main.asyncAfter(deadline: .now() + 3) { print("Dispatching after") } DispatchQueue.main.asyncAfter(deadline: .now() + 3, qos: .userInteractive, flags: []) { print("Dispatching after") } 

They both seems to work, are you sure that the problem isn't somewhere else?

Sign up to request clarification or add additional context in comments.

Comments

1

The compiler doesn’t seem to be able to infer the type of parameters in the asyncAfter call. So you might want to specify the type like this:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay) 

If you still get the same error make sure delay is of type DispatchTimeInterval. Here’s another example:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + DispatchTimeInterval.milliseconds(300) 

This should eventually silence the compiler.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.