将CUnsignedChar指针转换为CChar指针

时间:2017-01-08 10:45:56

标签: ios swift unsafe-pointers

我有一个客观的C函数,它给了我一个

UnsafePointer<UInt8>/UnsafePointer<CUnsignedChar>. 

但是我需要这些数据作为

UnsafePointer<Int8>

遗憾的是,我要求的功能

UnsafePointer<UInt8>.

1 个答案:

答案 0 :(得分:0)

在你的情况下,&#34;不安全的位投射&#34;将是最简单的解决方案:

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

另一种选择是&#34;记忆重新绑定&#34;:

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