将我的代码更新到 iOS 13 和 Swift 5,我收到此已弃用警告:
'withUnsafeBytes' is deprecated: use
withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
instead
通过这段代码从 SCNGeometry 中提取顶点:
/// Extracts vertices from SCNGeometry
func vertices() -> [SCNVector3] {
let vertexSources = self.sources(for: SCNGeometrySource.Semantic.vertex)
if let vertexSource = vertexSources.first {
let count = vertexSource.data.count / MemoryLayout<SCNVector3>.size
return vertexSource.data.withUnsafeBytes {
[SCNVector3](UnsafeBufferPointer<SCNVector3>(start: $0, count: count))
}
}
return []
}
问题出在这几行内:
return vertexSource.data.withUnsafeBytes {
[SCNVector3](UnsafeBufferPointer<SCNVector3>(start: $0, count: count))
}
This answer在这里发布了解决方案,但没有 Swift 5 代码,我无法提出警告的解决方案。
This other answer和 this thread似乎给出了一个解决方案,但仅适用于 UInt32
类型的值不是[SCNVector3]
(数组)。
谢谢大家,我被困住了。
最佳答案
您可以通过将这些行替换为来消除警告
return vertexSource.data.withUnsafeBytes { (buffer) in
[SCNVector3](buffer.bindMemory(to: SCNVector3.self))
}
也就是说此代码已损坏。永远不要假设 SCNGeometySource
中包含的数据布局(例如假设直接映射到 SCNVector3
)。 vectorCount
、floatComponents
、componentsPerVector
、bytesPerComponent
、dataOffset
和 dataStride
是正确检查属性源所必需的。
关于swift - 从 SceneKit iOS13 Swift 5 中提取顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59024564/