Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned Long Long from Double in Swift

I used to use the following in Objective-C:

double currentTime = CFAbsoluteTimeGetCurrent();

// self.startTime is called before, like     
// self.startTime = CFAbsoluteTimeGetCurrent();

double elapsedTime = currentTime - self.startTime;

// Convert the double to milliseconds
unsigned long long milliSecs = (unsigned long long)(elapsedTime * 1000);

In my swift code I have at the moment:

let currentTime: Double = CFAbsoluteTimeGetCurrent()
let elapsedTime: Double = currentTime - startTime

let milliSecs: CUnsignedLongLong = elapsedTime * 1000

However I get the error that a double cannot be converted to a CUnsignedLongLong which makes sense. Is there a way to cast it like in Objective-C though? Is there a way around this?

like image 206
GarethPrice Avatar asked Jul 27 '15 17:07

GarethPrice


Video Answer


1 Answers

Is there a way to cast it like in Objective C though? Is there a way around this?

let milliSecs = CUnsignedLongLong(elapsedTime * 1000)

Or

let milliSecs = UInt64(elapsedTime * 1000)
like image 59
simons Avatar answered Sep 18 '22 13:09

simons