C类型浮点的Swift 3 UnsafemeutablePointer初始化**
在Swift 3中,我需要向接受C类型浮点的Swift 3 UnsafemeutablePointer初始化**,c,swift3,unsafemutablepointer,C,Swift3,Unsafemutablepointer,在Swift 3中,我需要向接受float**输入的C对象发送数据 在Swift 2中,我曾声明一个unsafemtablepointer,构造一个Swift数组(仅用于init!),并将其传递给指针,它成功了: var bufferOut: UnsafeMutablePointer< UnsafeMutablePointer<Float32>>? arrayOut = Array(repeating: Array(repeating: 0, count:
float**
输入的C对象发送数据
在Swift 2中,我曾声明一个unsafemtablepointer
,构造一个Swift数组(仅用于init!),并将其传递给指针,它成功了:
var bufferOut: UnsafeMutablePointer< UnsafeMutablePointer<Float32>>?
arrayOut = Array(repeating: Array(repeating: 0, count: Int(size1), count: Int(size2))
bufferOut = UnsafeMutablePointer< UnsafeMutablePointer<Float32>>(arrayOut)
var bufferOut:UnsafeMutablePointer?
arrayOut=Array(重复:Array(重复:0,计数:Int(size1),计数:Int(size2))
bufferOut=UnsafeMutablePointer(arrayOut)
这一切都在Swift 3中被打破了
- 传递C样式的
并初始化它最快捷的方法是什么float**
- 在
中赋值的最佳方式是什么UnsafeMutablePointer
T**
应该使用autoreleasingusafmeutablepointer
,但实际上我从来没有构建过一个
请注意,我实际上并不关心上面示例中的数组!如果我可以直接使用已知容量初始化指针,我会
注意:描述了一些有用的情况,如C数组和C缓冲区,但是将这些方法转换为上述构造并不明显!以下是我最后完成的工作,并且正在工作。它遵循了在简单方案上显示示例程序的建议。总之,需要分配和分配每个插槽从顶级开始 因此,为了构造一个大小为(size1,size2)(类似于矩阵)的
UnsafeMutablePointer
,您可以使用一个称为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)
}
var-vectorBuf:UnsafeMutablePointer?
vectorBuf=UnsafeMutablePointer.allocate(容量:size2)
对于0中的索引…(size2){//had Int(channelCount)*
vectorBuf!.advanced(by:index).pointee=0.0
}
///这就是分配和初始化发生的地方:
bufferOut=UnsafeMutablePointer。分配(容量:Int(size1))
对于0…Int(大小1)中的索引{
bufferOut!.advanced(by:index).pointee=UnsafeMutablePointer.allocate(容量:(size2))
bufferOut!.advanced(by:index).pointee?.assign(from:vectorBuf!,count:size2)
}
希望这对其他尝试在C/C++中使用swift进行信号处理调用的人有用!以下是我最终完成的工作和正在进行的工作。它遵循在简单方案中显示示例程序的建议。总之,需要从顶层开始分配和分配每个插槽 因此,为了构造一个大小为(size1,size2)(类似于矩阵)的
UnsafeMutablePointer
,您可以使用一个称为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)
}
var-vectorBuf:UnsafeMutablePointer?
vectorBuf=UnsafeMutablePointer.allocate(容量:size2)
对于0中的索引…(size2){//had Int(channelCount)*
vectorBuf!.advanced(by:index).pointee=0.0
}
///这就是分配和初始化发生的地方:
bufferOut=UnsafeMutablePointer。分配(容量:Int(size1))
对于0…Int(大小1)中的索引{
bufferOut!.advanced(by:index).pointee=UnsafeMutablePointer.allocate(容量:(size2))
bufferOut!.advanced(by:index).pointee?.assign(from:vectorBuf!,count:size2)
}
希望这对其他尝试在C/C++中使用swift进行信号处理调用的人有用