0

我需要使用一个UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!用作参数的函数。它是在 Objective-C++ 框架中编写的,我通过桥接头访问它:

- (void) applyFilters: (float *) input_image input_params: (long *) input_params output_image : (float *) output_image output_params : (long *) output_params filter_id : (int) filter_id args : (float**) args

我在理解 args 参数的结构时遇到了一些麻烦,即float**. 在 Swift 中,它要求一个 type 的参数,UnsafeMutablePointer<UnsafeMutablePointer<Float>>我猜它指向一个浮点数组。

applyFilters(input_image: UnsafeMutablePointer<Float>!UnsafeMutablePointer<Float>!, input_params: UnsafeMutablePointer<Int>!, output_image: UnsafeMutablePointer<Float>!, output_params: UnsafeMutablePointer<Int>!, filter_id: Int32, args: UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!)

如果我创建一个浮点数组数组,我如何使用它来指向它,UnsafeMutablePointer<UnsafeMutablePointer<Float>>以便我可以将它传递给 C++ 函数?

4

1 回答 1

0

想出了如何做到这一点:

func arrayOfArrayExample() {

    let floatArray: [[Float]] = [
        [50.0, 23.0, 15.6],
        [24.0, 9.5, 12.7]
    ]

    let size1 = floatArray.count
    let size2 = floatArray[0].count

    var vectorBuf : UnsafeMutablePointer<Float>?
    let bufferOut = UnsafeMutablePointer< UnsafeMutablePointer<Float>?>.allocate(capacity: Int(size1))

    for i in 0..<size1 {
        vectorBuf = UnsafeMutablePointer<Float>.allocate(capacity: size2)
        for j in 0..<size2 {
            vectorBuf!.advanced(by: j).pointee = floatArray[i][j]
        }
        bufferOut.advanced(by: i).pointee = vectorBuf
    }

    defer {
        //deallocating memory
        for i in 0..<size1 {
            bufferOut.advanced(by: i).deallocate()
        }
    }
}

该变量bufferOut返回一个UnsafeMutablePointer<UnsafeMutablePointer<Float>>指向浮点数组数组的指针,然后释放内存。

于 2019-07-31T15:07:55.617 回答