'How can I cast UnsafePointer<UnsafeMutablePointer<Float>>? to UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!

I have an C API I need to interact with in Swift. One of the function takes an array of pointers as argument, which is imported by Swift as

  `UnsafeMutablePointer<UnsafeMutablePointer<Float>?>! `

The corresponding input on the Swift side is AVAudioPCMBuffer.floatChannelData, which is defined as

UnsafePointer<UnsafeMutablePointer<Float>>?

I am having trouble casting between the two. I tried to make it mutable by doing following:

UnsafeMutablePointer<UnsafeMutablePointer<Float>>(AVAudioPCMBuffer.floatChannelData)

And that did not work. The casting between Swift pointer types are very frustrating. Any help will be really appreciated.

Thanks



Solution 1:[1]

var testPtr : UnsafeMutablePointer<UnsafeMutablePointer<Float>?>! = nil
var testPtr2 : UnsafePointer<UnsafeMutablePointer<Float>>? = nil

testPtr = unsafeBitCast( testPtr2, to: UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!.self)

As per excellent @easytarget suggestion since XCode 13.3 use modern syntax instead (not generating a warning):

testPtr = UnsafeMutablePointer(mutating: testPtr2) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1