swift - 从 SceneKit iOS13 Swift 5 中提取顶点

标签 swift scenekit ios13 swift5 deprecation-warning

将我的代码更新到 iOS 13Swift 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 answerthis thread似乎给出了一个解决方案,但仅适用于 UInt32 类型的值不是[SCNVector3] (数组)

谢谢大家,我被困住了。

最佳答案

您可以通过将这些行替换为来消除警告

return vertexSource.data.withUnsafeBytes { (buffer) in
    [SCNVector3](buffer.bindMemory(to: SCNVector3.self))
}

也就是说此代码已损坏。永远不要假设 SCNGeometySource 中包含的数据布局(例如假设直接映射到 SCNVector3)。 vectorCountfloatComponentscomponentsPerVectorbytesPerComponentdataOffsetdataStride 是正确检查属性源所必需的。

关于swift - 从 SceneKit iOS13 Swift 5 中提取顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59024564/

相关文章:

ios - 如何设置 PFQuery 以使用 queryForTable 获取具有给定角色的所有用户?

ios - SceneKit 转向

ios - iOS 13.0 IONIC 中的视频转码问题

crash - 如何使用 [[UIApplication sharedApplication] valueForKey : @"statusBar"] on ios13? 获取 wifi 信号强度

swift - 如何从 SwiftUI 的官方教程代码中解释 VStack{ .. } ?

ios - Firebase 分页 - Swift 3

ios - 我希望 SWIFT/Scenekit 中的所有对象都在地板上有阴影

uicolor - iOS13:如何在 Assets 目录中为提升的用户界面级别指定颜色

ios - additionalSafeAreaInsets 的奇怪行为

swift - 将 SCNScene 分配给 SCNView - 在展开可选值时发现 nil