如何将UInt64转换为Int64?

尝试在Swift中调用dispatch_time实在令人难以理解,原因如下:

 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), {

doSomething()

})

结果为错误:“找不到接受提供的参数的’*’重载”。

NSEC_PER_SEC是UInt64,因此可以进行一些实验:

let x:UInt64 = 1000

let m:Int64 = 10 * x

结果与上述相同

let x:UInt64 = 1000

let m:Int64 = 10 * (Int64) x

“一行上的连续语句中的结果必须用’;’分隔”

let x:UInt64 = 1000

let m:Int64 = 10 * ((Int64) x)

结果为“预期的’,’分隔符”

let x:UInt64 = 1000

let m:Int64 = (Int64)10 * (Int64) x

“一行上的连续语句中的结果必须用’;’分隔”

等等

该死的Swift编译器,我放弃了。如何将UInt64强制转换为Int64,和/或如何快速使用dispatch_time?

回答:

将a强制转换UInt64为an Int64是不安全的,因为a UInt64可以具有大于的数字Int64.max,这将导致溢出。

以下是将转换UInt64Int64,反之亦然的代码段:

// Extension for 64-bit integer signed <-> unsigned conversion

extension Int64 {

var unsigned: UInt64 {

let valuePointer = UnsafeMutablePointer<Int64>.allocate(capacity: 1)

defer {

valuePointer.deallocate(capacity: 1)

}

valuePointer.pointee = self

return valuePointer.withMemoryRebound(to: UInt64.self, capacity: 1) { $0.pointee }

}

}

extension UInt64 {

var signed: Int64 {

let valuePointer = UnsafeMutablePointer<UInt64>.allocate(capacity: 1)

defer {

valuePointer.deallocate(capacity: 1)

}

valuePointer.pointee = self

return valuePointer.withMemoryRebound(to: Int64.self, capacity: 1) { $0.pointee }

}

}

这只是将二进制数据解释UInt64Int64,即Int64.max,由于64位整数的最高有效位处的符号位,其数字将大于负数。

如果只需要正整数,则获取绝对值。

根据行为,您可以获取绝对值,或:

if currentValue < 0 {

return Int64.max + currentValue + 1

} else {

return currentValue

}

后一种选项类似于剥离符号位。例如:

// Using an 8-bit integer for simplicity

// currentValue

0b1111_1111 // If this is interpreted as Int8, this is -1.

// Strip sign bit

0b0111_1111 // As Int8, this is 127. To get this we can add Int8.max

// Int8.max + currentValue + 1

127 + (-1) + 1 = 127

以上是 如何将UInt64转换为Int64? 的全部内容, 来源链接: utcz.com/qa/434322.html

回到顶部