swiftpointersswift3unsafe-pointersunsafemutablepointer

Convert UnsafeMutablePointer to UnsafePointer


I'm using a C library in my Swift project, and one of functions requires a UnsafePointer<UnsafePointer<UInt8>?>! as an in-out argument where I should pass my data. But the problem is that I have this data in UnsafeMutablePointer<UnsafeMutablePointer<UInt8>?>! type.

My question is - What is the most efficient and easiest way to convert UnsafeMutablePointer to UnsafePointer in Swift 3? Those pointers are not much different "on paper" and they shouldn't be much different from technical context, but I couldn't find valuable information on that topic.

Any help would be appreciated. Thanks


Solution

  • 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)