I am new to Swift and wanted to loop through an array of MKMapPoints<UnsafeMutablePointer>
which I get from an MKPolygon
by calling myPoly.points()
.
However, I am stuck as to how to loop through every element of the C-Array of pointers.
for element in myPointsArray {}
does not work and I don't know how to determine the number of elements of this kind of array in Swift. Any ideas? Thanks for your help!
UnsafeBufferPointer
presents an unsafe pointer and a count as a collection so you can for..in over it, subscript it safely, pass it to algorithms that work on collections etc:
for point in UnsafeBufferPointer(start: poly.points(), count: poly.pointCount) {
println("\(point.x),\(point.y)")
}
You can get the count of points from MKPolygon.pointCount
property. And iterate points
with traditional for ; ; {}
loop:
let myPointsArray = myPoly.points()
for var i = 0, len = myPoly.pointCount; i < len; i++ {
let point = myPointsArray[i]
println(point)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With