如何从Swift数组中删除此对象?

时间:2017-04-02 19:58:55

标签: swift cllocationcoordinate2d

从数组中删除formattedCoordinate的适当命令是什么?我知道Objective-C存在一个removeObject,但是Swift呢?

var markersArray: Array<CLLocationCoordinate2D> = []

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) need to fix this
    self.clean()
    //    return 0; not sure if we need this here
}

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) need to fix this
}

1 个答案:

答案 0 :(得分:1)

我建议

self.markersArray = self.markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })