问题描述
在 Swift 3 中,我需要将数据发送到接受 float **
输入的 C 对象.
Within Swift 3, I need to send data to a C object accepting float **
input.
在 Swift 2 中,我曾经声明过一个 UnsafeMutablePointer<UnsafeMutablePointer<Float32>>
,构造一个 swift 数组(仅用于 init!),并将其传递给指针,它起作用了:
In Swift 2, I used to declare a UnsafeMutablePointer< UnsafeMutablePointer<Float32>>
, construct a swift Array (only for init!), and pass this to the pointer, and it worked:
var bufferOut: UnsafeMutablePointer< UnsafeMutablePointer<Float32>>?
arrayOut = Array(repeating: Array(repeating: 0, count: Int(size1), count: Int(size2))
bufferOut = UnsafeMutablePointer< UnsafeMutablePointer<Float32>>(arrayOut)
这一切在 Swift 3 中都被打破了!
This is all broken now in Swift 3!
- 传递 C 样式
float**
并初始化它的最快捷方式是什么? - 在
UnsafeMutablePointer< 中分配值的最佳方法是什么?UnsafeMutablePointer>
?
- What is the most Swifty way to pass a C-Style
float**
and initialize it? - What would be the best way to assign values in a
UnsafeMutablePointer< UnsafeMutablePointer<T>>
?
文档说对于 T **
应该使用 AutoreleasingUnsafeMutablePointer<T>
但我实际上从未设法构造一个!
Documentations say that for T **
one should use AutoreleasingUnsafeMutablePointer<T>
but I actually never managed to construct one!
请注意,我实际上并不关心上面示例中的 Array!如果我可以直接使用已知容量初始化指针,我会的.
Note that I don't actually care for the Array in the above example! If I could just initialize the Pointer directly using known capacities, I would.
注意:UnsafeRawPointer 的预期用例部分 描述了有用的情况,例如 C 数组和 C 缓冲区,但是为上述构造转换此类方法并不明显!
Note: The Expected use cases Section of UnsafeRawPointer describes useful situations such C array and C Buffers, however translating such methods for the above construct is not evident!
推荐答案
以下是我最终完成的工作.它遵循 新的 UnsafeMuTablePointer 参考 显示示例的建议简单计划的程序.总之,需要从顶层开始分配和分配每个槽!
Here is what I ended up doing and is working. It follows recommendations from new UnsafeMuTablePointer Reference showing example programs on simple schemes. In summary, it is required to allocate and assign each slot starting from top-level!
所以为了构造一个UnsafeMutablePointer 的大小为 (size1, size2)(如矩阵),您可以使用此处称为
vectorBuf
的中间向量进行如下操作:
So in order to construct an UnsafeMutablePointer< UnsafeMutablePointer<T>>
of size (size1, size2) (like a matrix), you can go ahead as follows using an intermediate vector here called vectorBuf
:
var vectorBuf : UnsafeMutablePointer<T>?
vectorBuf = UnsafeMutablePointer<T>.allocate(capacity: size2)
for index in 0...(size2) { // had Int(channelCount)*
vectorBuf!.advanced(by: index).pointee = 0.0
}
/// This is where allocation and initialization happens:
bufferOut = UnsafeMutablePointer< UnsafeMutablePointer<T>?>.allocate(capacity: Int(size1))
for index in 0...Int(size1) {
bufferOut!.advanced(by: index).pointee = UnsafeMutablePointer<T>.allocate(capacity: (size2) )
bufferOut!.advanced(by: index).pointee?.assign(from: vectorBuf!, count: size2)
}
希望这对其他尝试在 C/C++ 中使用 swift 进行信号处理调用的人有用!
Hope this can be useful to others trying to use swift with signal processing calls in C/C++!
这篇关于C 类型浮点数的 Swift 3 UnsafeMutablePointer 初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!