Try to search your question here, if you can't find : Ask Any Question Now ?

How to fix Cannot invoke initializer for type 'dispatch_time_t' with an argument list of type '(DispatchTime, Int64)'

HomeCategory: stackoverflowHow to fix Cannot invoke initializer for type 'dispatch_time_t' with an argument list of type '(DispatchTime, Int64)'
Avatargaurav asked 4 months ago

I’m working on an old Swift 2-based tutorial that is a word matching(anagram) game to learn more about Swift (https://www.raywenderlich.com/2185-how-to-make-a-letter-word-game-with-uikit-and-swift-part-3-3) and I’m running into an issue where when write code to try to use Grand Central Dispatch (GCD) to dispatch some block objects to disable an emitter and to remove it from its superview. The Error message: Cannot invoke initializer for type ‘dispatch_time_t’ with an argument list of type ‘(DispatchTime, Int64)’

Since the code from the tutorial is in Swift 2, I tried to convert some of the syntax to Swift 4.2 (Ex: DISPATCH_TIME_NOW to DispatchTime.now()) However, I then encounter other errors leading to the present one.

This is the original code from the tutorial. It’s within an override function called didMoveToSuperview():

//disable the emitter
var delay = Int64(0.1 * Double(NSEC_PER_SEC))
var delayTime = dispatch_time(DISPATCH_TIME_NOW, delay)
dispatch_after(delayTime, dispatch_get_main_queue()) {
  self.disableEmitterCell()
}

//remove explosion view
delay = Int64(2 * Double(NSEC_PER_SEC))
delayTime = dispatch_time(DISPATCH_TIME_NOW, delay)
dispatch_after(delayTime, dispatch_get_main_queue()) {
  self.removeFromSuperview()
}

And this is what I’ve tried to do:

//disable the emitter
        var delay = Int64(0.1 * Double(NSEC_PER_SEC))
        var delayTime = dispatch_time_t(DispatchTime.now(), delay)
        dispatch_after(delayTime, dispatch_get_main_queue()) {
            self.disableEmitterCell()
        }

        //remove explosion view
        delay = Int64(2 * Double(NSEC_PER_SEC))
        delayTime = dispatch_time_t(DispatchTime.now(), delay)
        dispatch_after(delayTime, dispatch_get_main_queue()) {
            self.removeFromSuperview()
        }

This is an except from the tutorial page : https://www.raywenderlich.com/2185-how-to-make-a-letter-word-game-with-uikit-and-swift-part-3-3

Here you are using Grand Central Dispatch to dispatch blocks to disable the emitter and remove it from its superview. First you schedule a delay of 1/10th of a second to disable the emitter, and after a delay of 2 seconds, you make another call to remove the explosion view from its parent. Why not just remove the view? You want to let the exploded particles fly away and dissolve before killing the effect.

1 Answers
Best Answer
AvatarJyoti answered 4 months ago
Your Answer

12 + 13 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com