如何将 UnsafeMutablePointer<Double> 数组转换为 Double 数组?
How do I convert an array of UnsafeMutablePointer<Double> to an array of Double?
我创建了一个这样的数组
var outputReal = UnsafeMutablePointer<Double>.allocate(capacity: numeroDados)
现在我需要将其转换为 Double
.
的数组
我可以用这样的东西转换它:
var newArray : [Double] = []
for i in 0..<n {
newArray[i] = outputReal
}
但我记得在某个页面上看到过另一种方法。
有什么想法吗?
您的意思是将 Pointer to double 转换为 double 数组吗??
您不应将指向 double 的指针转换为 swift 中的 double 数组,因为 swift 中的数组不一定与 C 数组相同。相反,您将内容复制到新数组中。一个 "low-level" 的方法是
var pointerToDoubles = UnsafeMutablePointer<Double>.allocate(capacity: 10)
var newArray = Array<Double>(repeating: 0, count: 10)
_ = newArray.withContiguousMutableStorageIfAvailable {
UnsafeMutableRawPointer([=10=].baseAddress!).copyMemory(from: x, byteCount: MemoryLayout<double>.size * 10)
}
请记住这是不安全的,如果您犯了一些错误将会崩溃,例如您的新数组太小或者您的指针没有 as许多你期待的元素。
首先创建一个引用相同内存区域的UnsafeBufferPointer<Double>
。那是一个 Collection
以便您可以从该内存创建和初始化一个数组:
let bufPtr = UnsafeBufferPointer(start: outputReal, count: numeroDados)
let newArray = Array(bufPtr)
或者,首先分配一个UnsafeMutableBufferPointer
(它包含分配内存的地址和大小):
let numeroDados = 10
let outputReal = UnsafeMutableBufferPointer<Double>.allocate(capacity: numeroDados)
outputReal.initialize(repeating: 0.0)
let newArray = Array(outputReal)
在这两种情况下,值都复制到数组的元素存储中,因此您最终必须释放分配的内存。
详情
- Xcode 11.2.1 (11B500), Swift 5.1
解决方案 1
extension UnsafeMutablePointer {
func toArray(capacity: Int) -> [Pointee] {
return Array(UnsafeBufferPointer(start: self, count: capacity))
}
}
解决方案1的用法
var array = [3.9, 7.7, 11.1, 1.11, 1.02, 3.3, 3.9, 0]
let pointer = UnsafeMutablePointer<Double>.allocate(capacity: array.count)
pointer.initialize(from: &array, count: array.count)
print(pointer.toArray(capacity: array.count))
解决方案 2
extension Array {
init(pointer: UnsafeMutablePointer<Element>, count: Int) {
let bufferPointer = UnsafeBufferPointer<Element>(start: pointer, count: count)
self = Array(bufferPointer)
}
}
解决方案2的用法
var array2 = [3.9, 7.7, 11.1, 1.11, 1.02, 3.3, 3.9, 0]
let pointer2 = UnsafeMutablePointer<Double>.allocate(capacity: array.count)
pointer2.initialize(from: &array, count: array.count)
print(Array(pointer: pointer2, count: array.count))
我创建了一个这样的数组
var outputReal = UnsafeMutablePointer<Double>.allocate(capacity: numeroDados)
现在我需要将其转换为 Double
.
我可以用这样的东西转换它:
var newArray : [Double] = []
for i in 0..<n {
newArray[i] = outputReal
}
但我记得在某个页面上看到过另一种方法。
有什么想法吗?
您的意思是将 Pointer to double 转换为 double 数组吗??
您不应将指向 double 的指针转换为 swift 中的 double 数组,因为 swift 中的数组不一定与 C 数组相同。相反,您将内容复制到新数组中。一个 "low-level" 的方法是
var pointerToDoubles = UnsafeMutablePointer<Double>.allocate(capacity: 10)
var newArray = Array<Double>(repeating: 0, count: 10)
_ = newArray.withContiguousMutableStorageIfAvailable {
UnsafeMutableRawPointer([=10=].baseAddress!).copyMemory(from: x, byteCount: MemoryLayout<double>.size * 10)
}
请记住这是不安全的,如果您犯了一些错误将会崩溃,例如您的新数组太小或者您的指针没有 as许多你期待的元素。
首先创建一个引用相同内存区域的UnsafeBufferPointer<Double>
。那是一个 Collection
以便您可以从该内存创建和初始化一个数组:
let bufPtr = UnsafeBufferPointer(start: outputReal, count: numeroDados)
let newArray = Array(bufPtr)
或者,首先分配一个UnsafeMutableBufferPointer
(它包含分配内存的地址和大小):
let numeroDados = 10
let outputReal = UnsafeMutableBufferPointer<Double>.allocate(capacity: numeroDados)
outputReal.initialize(repeating: 0.0)
let newArray = Array(outputReal)
在这两种情况下,值都复制到数组的元素存储中,因此您最终必须释放分配的内存。
详情
- Xcode 11.2.1 (11B500), Swift 5.1
解决方案 1
extension UnsafeMutablePointer {
func toArray(capacity: Int) -> [Pointee] {
return Array(UnsafeBufferPointer(start: self, count: capacity))
}
}
解决方案1的用法
var array = [3.9, 7.7, 11.1, 1.11, 1.02, 3.3, 3.9, 0]
let pointer = UnsafeMutablePointer<Double>.allocate(capacity: array.count)
pointer.initialize(from: &array, count: array.count)
print(pointer.toArray(capacity: array.count))
解决方案 2
extension Array {
init(pointer: UnsafeMutablePointer<Element>, count: Int) {
let bufferPointer = UnsafeBufferPointer<Element>(start: pointer, count: count)
self = Array(bufferPointer)
}
}
解决方案2的用法
var array2 = [3.9, 7.7, 11.1, 1.11, 1.02, 3.3, 3.9, 0]
let pointer2 = UnsafeMutablePointer<Double>.allocate(capacity: array.count)
pointer2.initialize(from: &array, count: array.count)
print(Array(pointer: pointer2, count: array.count))