[英]How to access only Y coordinates of CGPoints in array?
我有20個CGPoint的數組。 如何僅訪問數組中每個CGPoint的Y坐標?
var arrayOfPoints : [CGPoint] = [.....]//your array of points
for point in arrayOfPoints {
let y = point.y
//You now have just the y coordinate of each point in the array.
}
或者,如果您使用的是.enumerate()
語法。
for (index, point) in arrayOfPoints.enumerate() {
let y = point.y
//You now have just the y coordinate of each point in the array.
print(point.y) //Prints y coordinate of each point.
}
Swift使for
循環操作變得簡單。 例如,如果要一個包含所有y坐標的數組,則可以快速使用一個漂亮的襯板。
let arrayOfYCoordinates : [CGFloat] = arrayOfPoints.map { $0.y }
或者傳入以將每個y coordiante傳遞給相同的函數。
arrayOfPoints.map { myFunction($0.y) }
干得好
let arrayOfPoints : [CGPoint] = [CGPoint(x: 1, y: 2), CGPoint(x: 3, y: 4)]
let yCoordinates = arrayOfPoints.map { $0.y }
for y in yCoordinates {
print("y = \(y)") //Or whatever you want to do with the y coordinates
}
為什么不使用point.y
工作一個簡單的foreach循環?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.