gpt4 book ai didi

ios - 如何根据值从 Swift 数组中删除元素?

转载 作者:行者123 更新时间:2023-11-30 12:33:44 25 4
gpt4 key购买 nike

我有以下代码:

func mapView(mapView: GMSMapView, didTap marker: GMSMarker){
let lat: CLLocationDegrees = marker.position.latitude
let lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray.remove(formattedCoordinate)
self.clean()
}

func mapView(mapView: GMSMapView, didBeginDragging marker: GMSMarker){
let lat: CLLocationDegrees = marker.position.latitude
let lng: CLLocationDegrees = marker.position.longitude
var formattedCoordinate = CLLocationCoordinate2D(latitude: lat,longitude: lng)
markersArray.remove(formattedCoordinate)
}

正如你所看到的,我正在尝试从markersArray中“删除”“formattedCooperative”。我看到有像 .filter 和其他方法这样的选项可以删除特定索引处的元素,但在这里我想从数组中删除该特定坐标。我该怎么做?

最佳答案

    markers = markers.filter({ (coordinateToRemove) -> Bool in
return coordinateToRemove.latitude == formattedCoordinate.latitude && coordinateToRemove.longitude == formattedCoordinate.longitude
})

我遇到了同样的问题,通过四舍五入您正在比较的纬度和经度的值(因为它们在formattedCooperative对象中可能存在小数点差异)来解决它使用

圆形(坐标删除.纬度)

并分别比较它们。

关于ios - 如何根据值从 Swift 数组中删除元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43133831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com