hotpaw2 hotpaw2 - 1 year ago 126
Swift Question

Creating an UnsafeMutablePointer<UnsafeMutablePointer<Float> parameter in Swift 3

The Swift signature of the Accelerate framework vDSP_biquadm() function includes parameter types of UnsafeMutablePointer> and UnsafeMutablePointer> .

How does one declare and create such types in Swift 3, and then fill these pointer arrays with references to several Swift arrays of type [Float] .

Answer Source

When you need to pass multiple values for UnsafeMutablePointer<T> parameters, you need to declare a variable of type [T] (aka Array<T>) and pass it as an inout argument. In your case T is UnsafeMutablePointer<Float>.

So, if you want to start with [Float], you may need to write something like this:

let input: [Float] = [/*...*/]
var output: [Float] = Array(repeating: 0, count: outputTotalSize)
input.withUnsafeBufferPointer {inBuf in
    let inputPtr = inBuf.baseAddress!
    output.withUnsafeMutableBufferPointer {outBuf in
        let outputPtr = outBuf.baseAddress!
        var pInputs: [UnsafePointer<Float>] = [inputPtr,/*...*/]
        var pOutputs: [UnsafeMutablePointer<Float>] = [outputPtr/*...*/]
        vDSP_biquadm(setup, &pInputs, inStride, &pOutputs, outStride, length)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download