Create dispatch_time_t with NSTimeInterval

I have an NSTimeInterval value, I need to create a dispatch_time_t value with it. This is what I tried:

 let timeInterval : NSTimeInterval = getTimeInterval() //ERROR: Binary operator '*' cannot be applied to operands of type 'NSTimeInterval' and 'UInt64' let dispatch_time = dispatch_time(DISPATCH_TIME_NOW, Int64(timerInterval * NSEC_PER_SEC)) 

I understand this error message, but I do not know how to get rid of it. Could you offer some suggestions? How to instantiate dispatch_time using NSTimeInterval ? Thanks!

+6
source share
2 answers
 let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.3 * Double(NSEC_PER_SEC))) dispatch_after(delayTime, dispatch_get_main_queue()) { //... Code } 

You can try this, it works fine with me.

In your code, simply replace your last line as follows:

 let d_time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeInterval * Double(NSEC_PER_SEC))) 
+8
source

You have:

let timeInterval: NSTimeInterval = getTimeInterval ()

let dispatch_time = dispatch_time (DISPATCH_TIME_NOW, Int64 (timeInterval * NSEC_PER_SEC))

And you get the following error:

 ERROR: Binary operator '*' cannot be applied to operands of type 'NSTimeInterval' and 'UInt64' 

As a result, you will need to translate or change the types of variables in your Int64(timeInterval * NSEC_PER_SEC) equation Int64(timeInterval * NSEC_PER_SEC) so that they have compatible data types.

  • timeInterval is an NSTimeInterval that is an alias of type Double
  • NSEC_PER_SEC - UInt64
  • dispatch_time function expects an Int64 argument

Therefore, the error will disappear by changing the value of NSEC_PER_SEC to a Double so that it matches the data type timeInterval .

 let dispatch_time = dispatch_time(DISPATCH_TIME_NOW, Int64(timeInterval * Double(NSEC_PER_SEC))) 

Another random point: you will probably get the Variable used within its own initial value error when you name your dispatch_time variable when you call dispatch_time .

+1
source

All Articles