I am new to Swift and wanted to loop through an array of MKMapPoints 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.
我是Swift的新手,想要遍历一个MKMapPoints数组,我通过调用myPoly.points()从MKPolygon获取这些数组。但是,我不知道如何循环遍历指针C-Array的每个元素。
for element in myPointsArray {}
does not work and I don't know how determine the number of elements of this kind of array in Swift. Any ideas? Thanks for your help!
不起作用,我不知道如何确定Swift中这种数组的元素数量。有任何想法吗?谢谢你的帮助!
2 个解决方案
#1
11
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:
UnsafeBufferPointer将一个不安全的指针和一个计数作为一个集合呈现,这样你就可以在它上面安全地下标它,将它传递给处理集合的算法等:
for point in UnsafeBufferPointer(start: poly.points(), count: poly.pointCount) {
println("\(point.x),\(point.y)")
}
#2
2
You can get the count of points from MKPolygon.pointCount
property. And iterate points
with traditional for ; ; {}
loop:
您可以从MKPolygon.pointCount属性获取点数。并用传统的迭代点; ; {}循环:
let myPointsArray = myPoly.points()
for var i = 0, len = myPoly.pointCount; i < len; i++ {
let point = myPointsArray[i]
println(point)
}
#1
11
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:
UnsafeBufferPointer将一个不安全的指针和一个计数作为一个集合呈现,这样你就可以在它上面安全地下标它,将它传递给处理集合的算法等:
for point in UnsafeBufferPointer(start: poly.points(), count: poly.pointCount) {
println("\(point.x),\(point.y)")
}
#2
2
You can get the count of points from MKPolygon.pointCount
property. And iterate points
with traditional for ; ; {}
loop:
您可以从MKPolygon.pointCount属性获取点数。并用传统的迭代点; ; {}循环:
let myPointsArray = myPoly.points()
for var i = 0, len = myPoly.pointCount; i < len; i++ {
let point = myPointsArray[i]
println(point)
}