Double in Swift 中的 Unsigned Long Long

Unsigned Long Long from Double in Swift

我曾经在 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);

在我目前的 swift 代码中:

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

let milliSecs: CUnsignedLongLong = elapsedTime * 1000

但是我收到错误消息,即 double 无法转换为 CUnsignedLongLong,这是有道理的。有没有办法像 Objective-C 那样投射它?有解决办法吗?

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

let milliSecs = CUnsignedLongLong(elapsedTime * 1000)

或者

let milliSecs = UInt64(elapsedTime * 1000)

CUnsignedLongLong 在标准库中定义为:

typealias CUnsignedLongLong = UInt64

因此,要将 Double 转换为 CUnsignedLongLong,您需要使用

创建 CUnsignedLongLong 的新实例
CUnsignedLongLong(elapsedTime * 1000)

UInt64(elapsedTime * 1000)

Swift 不允许隐式类型转换。使用其类型的构造函数创建值。

let milliSecs = CFUnsignedLongLong(elapsedTime * 1000)