将UnsafeMutablePointer转换为UnsafePointer

时间:2018-02-21 20:45:23

标签: swift pointers swift3 unsafe-pointers unsafemutablepointer

我在我的Swift项目中使用了一个C库,其中一个函数需要UnsafePointer<UnsafePointer<UInt8>?>!作为输入参数,我应该传递我的数据。但问题是我在UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>!类型中有这些数据。

我的问题是 - 在Swift 3中将UnsafeMutablePointer转换为UnsafePointer的最有效,最简单的方法是什么?那些指针在“纸上”并没有太大的不同,它们应该与技术背景没有太大的不同,但我找不到有关该主题的有价值的信息。

任何帮助将不胜感激。感谢

1 个答案:

答案 0 :(得分:4)

UnsafePointers can be initialized with an UnsafeMutablePointer

// Any UnsafeMutablePointer with capacity one
let ump = UnsafeMutablePointer<Any>.allocate(capacity: 1)
// `up` now contains an UnsafePointer initialized with `ump`
var up = UnsafePointer(ump)