简体   繁体   English

如何遍历数组的元素<UnsafeMutablePointer>在斯威夫特

[英]how to loop through elements of array of <UnsafeMutablePointer> in Swift

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() .我是 Swift 的新手,想遍历我通过调用myPoly.points()MKPolygon获取的MKMapPoints<UnsafeMutablePointer>数组。 However, I am stuck as to how to loop through every element of the C-Array of pointers.但是,我不知道如何遍历 C 指针数组的每个元素。

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.不起作用,我不知道如何在 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: UnsafeBufferPointer表示一个不安全的指针和一个作为集合的计数,这样你就可以 for..in 覆盖它,安全地给它下标,将它传递给处理集合的算法等:

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.您可以从MKPolygon.pointCount属性获取点数。 And iterate points with traditional for ; ; {}并用传统的for ; ; {}迭代points for ; ; {} for ; ; {} for ; ; {} loop: for ; ; {}循环:

let myPointsArray = myPoly.points()

for var i = 0, len = myPoly.pointCount; i < len; i++ {
    let point = myPointsArray[i]
    println(point)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM