I've been saying this to form a C array of CGPoint:
let arr = UnsafeMutablePointer<CGPoint>.allocate(capacity:4)
defer {
arr.deinitialize()
arr.deallocate(capacity:4)
}
arr[0] = CGPoint(x:0,y:0)
arr[1] = CGPoint(x:50,y:50)
arr[2] = CGPoint(x:50,y:50)
arr[3] = CGPoint(x:0,y:100)
Now (Swift 4.1 in the Xcode 9.3 beta) both deinitialize
and deallocate(capacity:)
are deprecated. It looks like what I'm supposed to say now might be:
defer {
arr.deinitialize(count:4)
arr.deallocate()
}
Is that right?
.deinitialize
asCGPoint
is a trivial type (if it wasn't a trivial type, i.e contained references, then you'd have to initialise the allocated memory before performing assignment, and then deinitialise afterwards). – Responsibilitydeinitialize
just in case. – Michaelinedeinitialize
because why not – Michaeline.initialize
methods (before assignment through subscript, that is). You could also use a buffer pointer (gist.github.com/hamishknight/57320e2dbfbd0ad3ef68c69a97830eac). – ResponsibilitybaseAddress
. But my code doesn't do that. So is my code wrong? I'm not getting any closer to clarity or certainty. – MichaelinebaseAddress
). The deinitialisation of thebaseAddress
in my code is equivalent to thearr.deinitialize(count:4)
that you do in your code. Buffer pointers don't currently expose adeinitialize()
method, so that's why we have to extract the pointer and call it ourselves. – Responsibilitylet arr = [CGPoint(x:0,y:0), ...]
directly? You still can pass that to a C function, if necessary. – Humorous