将 CUnsignedChar 指针转换为 CChar 指针

Convert CUnsignedChar pointer to CChar pointer

我有一个 objective C 函数,可以

UnsafePointer<UInt8>/UnsafePointer<CUnsignedChar>. 

但是我需要这些数据作为

UnsafePointer<Int8>

不幸的是我需要的功能没有

UnsafePointer<UInt8>.

在您的情况下,"unsafe bit cast" 将是最简单的解决方案:

let ptr: UnsafePointer<UInt8> = ...
let i8ptr = unsafeBitCast(ptr, to: UnsafePointer<Int8>.self)

另一种选择是"memory rebinding":

ptr.withMemoryRebound(to: Int8.self, capacity: <count>) { i8ptr in
    // ...
}