Swift 3中的SCNGeometryElement设置 [英] SCNGeometryElement setup in Swift 3

查看:152
本文介绍了Swift 3中的SCNGeometryElement设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我忍无可忍,开始将我的应用程序转换为Swift3。与往常一样,转换器有很多不足之处。在这种情况下,我不确定如何正确编码新版本。原来是这样:

I bit the bullet and started converting my app to Swift 3. As always, the converter leaves very much to be desired. In this case, I'm not sure how to properly code the new version. Here is the original:

let indexes : [CInt] = [0,1,2,3]
let dat  = NSData(bytes: indexes, length: sizeofValue(indexes))
let ele = SCNGeometryElement(data:dat, primitiveType: .Triangles, primitiveCount: 2, bytesPerIndex: sizeof(Int))

运行转换并编写新的sizeof(谢谢)后,我最终得到了这一点:

After running the conversion and writing a new sizeof (thanks), I ended up with this:

let indexes : [CInt] = [0,1,2,3]
let dat  = Data(bytes: UnsafePointer<UInt8>(indexes), count: sizeof(indexes))
let ele = SCNGeometryElement(data:dat, primitiveType: .triangles, primitiveCount: 2, bytesPerIndex: MemoryLayout<Int>.size)

但是,这给了我(在 Data(bytes:length:)调用中):

However, this gives me (on the Data(bytes:length:) call):


'init'不可用:使用'withMemoryRebound(to:capacity:_)'临时将内存视为另一种与布局兼容的类型。 / p>

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

我查看了一些线程在这里,并阅读了涵盖此内容的发行说明,但我仍然对应该在此处执行的操作感到困惑。

I've looked over a few threads here, and read the release notes that cover this, and I'm still baffled what I'm supposed to do here.

推荐答案

您修复了一个 sizeof 而不是另一个,并且您正在创建一个不需要此指针的新指针-任何数组(给出正确的元素类型)都可以传递给采用C样式指针的API。这样,您的代码的直接解决方法是:

You fixed one sizeof but not the other, and you're creating a new pointer where such is unnecessary — any array (given the right element type) can be passed to APIs that take C-style pointers. The direct fix for your code is then:

let indexes: [CInt] = [0,1,2,3]
let dat = Data(bytes: indexes, count: MemoryLayout<CInt>.size * indexes.count)
let ele = SCNGeometryElement(data:dat, primitiveType: .triangles, primitiveCount: 2, bytesPerIndex: MemoryLayout<CInt>.size)

(另请注意使您的 MemoryLayout修复的方法与它们描述的数据保持一致。)

(Note also the fixes to make your MemoryLayouts consistent with the data they describe.)

但是,除非您对额外的 Data有一定的需求对象,为了使指针更有趣或在描述元素时更加特殊,可以使用以下简单形式:

However, unless you have some need for the extra Data object, for fun with pointers, or for the extra specificity in describing your element, you can use the simpler form:

let indices: [UInt8] = [0,1,2,3] 
let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)

此通用初始化程序自动管理内存ory,推断数组的数量,并根据数组的数量和 primitiveType primitiveCount $ c>您指定。

This generic initializer automatically manages memory on the way in, infers the count of the array, and infers the primitiveCount based on the count of the array and the primitiveType you specify.

(请注意,对于 .triangles ,四个索引数组是一个不寻常的数字;要么有一个三角形,一个未使用的索引,或者您实际上是指包含两个基元的 .triangleStrip 。)

(Note that an array of four indices is an unusual number for .triangles; either you have one triangle and one unused index, or you actually mean a .triangleStrip containing two primitives.)

这篇关于Swift 3中的SCNGeometryElement设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆