How can I interact with functions in swift that used to take sized C arrays?
I read through Interacting with C APIS and still can\'t figure this out.
The doc
Normally you can just pass an array of the required type as an in-out parameter, aka
var coords: [CLLocationCoordinate2D] = []
polyline.getCoordinates(&coords, range: NSMakeRange(0, polyline.pointCount))
but that documentation makes it seem like a bad idea! Luckily, UnsafeMutablePointer
provides a static alloc(num: Int)
method, so you can call getCoordinates()
like this:
var coordsPointer = UnsafeMutablePointer.alloc(polyline.pointCount)
polyline.getCoordinates(coordsPointer, range: NSMakeRange(0, polyline.pointCount))
To get the actual CLLocationCoordinate2D
objects out of the mutable pointer, you should be able to just loop through:
var coords: [CLLocationCoordinate2D] = []
for i in 0..
And since you don't want a memory leak, finish up like so:
coordsPointer.dealloc(polyline.pointCount)
Just remembered Array
has a reserveCapacity()
instance method, so a much simpler (and probably safer) version of this would be:
var coords: [CLLocationCoordinate2D] = []
coords.reserveCapacity(polyline.pointCount)
polyline.getCoordinates(&coords, range: NSMakeRange(0, polyline.pointCount))