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))
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)
However, this gives me (on the Data(bytes:length:)
call):
'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.
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:(Note also the fixes to make your
MemoryLayout
s consistent with the data they describe.)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: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 theprimitiveType
you specify.(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.)