简体   繁体   English

如何更新Swift 3 UnsafePointer的数据语句?

[英]How do I update a statement of Data of UnsafePointer for Swift 3?

I'm getting the error below for this statement: 我收到此语句的以下错误:

 let data = Data(bytes: UnsafePointer<UInt8>(cubeData), count: cubeData.count * MemoryLayout<Float>.size)

cubeData is defined as: var cubeData = [Float](repeating: 0, count: size * size * size * 4) cubeData定义为: var cubeData = [Float](repeating: 0, count: size * size * size * 4)

Error: 错误:

 'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

How can I fix this? 我怎样才能解决这个问题?

Thanks! 谢谢!

You could use Array.withUnsafeBufferPointer to obtain a buffer pointer (ie a pointer to an array with its length). 您可以使用Array.withUnsafeBufferPointer获得缓冲区指针(即,指向具有其长度的数组的指针)。 Then use Data.init(buffer:) to initiate the data from a buffer pointer. 然后使用Data.init(buffer:)从缓冲区指针初始化数据。

let cubeData: [Float] = [1.1, 2.2, 3.3, 4.4]

let b = cubeData.withUnsafeBufferPointer { Data(buffer: $0) }

print(b as NSData)
// <cdcc8c3f cdcc0c40 33335340 cdcc8c40>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Swift中的void UnsafePointer中提取数据? - how do I extract data from void UnsafePointer in Swift? 如何在Swift中访问UnsafePointer引用的结构的成员字段的UnsafePointer? - How do I access UnsafePointer of member field of a struct referenced by an UnsafePointer in Swift? 如何在Swift 3中使用UnsafePointer - How to use UnsafePointer with Swift 3 如何将我的swift类转换为与objective-c中的__bridge类似的UnsafePointer - How do I convert my swift class to an UnsafePointer like the __bridge in objective-c 如何从不可变的非类值中获取Swift UnsafePointer? - How do I get a Swift UnsafePointer from an immutable non-class value? 如何在Swift中正确初始化UnsafePointer? - How to correctly initialize an UnsafePointer in Swift? 如何安全正确地将 [[T]] 转换为 UnsafePointer <UnsafePointer<T> &gt; 暂时 - How do I safely and correctly convert [[T]] to UnsafePointer<UnsafePointer<T>> temporarily 如何提取 UnsafePointer<cgfloat> 来自 UnsafePointer<cgpoint> - Swift</cgpoint></cgfloat> - How to extract UnsafePointer<CGFloat> from UnsafePointer<CGPoint> - Swift Swift 3 中的 UnsafePointer - UnsafePointer in Swift 3 如何转换UnsafePointer &lt;[Float]&gt;或将Float Array数据拟合到Unsafe [Mutable]指针 <Float> 在Swift 3中? - How to convert UnsafePointer<[Float]> or fit Float Array data to Unsafe[Mutable]Pointer<Float> in Swift 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM