将浮点数数组转换为 UnsafeMutablePointer<UnsafeMutablePointer<Float>>
Turn an array of array of float numbers into UnsafeMutablePointer<UnsafeMutablePointer<Float>>
我需要使用一个以 UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!
作为参数的函数。它是在 Objective-C++ 的框架中编写的,我通过桥接 header:
访问它
- (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 中,它要求一个类型为 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++ 函数?
想出如何去做:
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
returns一个UnsafeMutablePointer<UnsafeMutablePointer<Float>>
指向Float数组的指针,之后释放内存。
我需要使用一个以 UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!
作为参数的函数。它是在 Objective-C++ 的框架中编写的,我通过桥接 header:
- (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 中,它要求一个类型为 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++ 函数?
想出如何去做:
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
returns一个UnsafeMutablePointer<UnsafeMutablePointer<Float>>
指向Float数组的指针,之后释放内存。