2017-04-02 64 views
1

从数组中删除formattedCoordinate的适当命令是什么?我知道一个removeObject存在Objective-C,但Swift怎么样?如何从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 
} 
+0

棒的位置:在与删除您行formattedCoordinate前 –

+0

查看收藏迅速语种导游部分:https://developer.apple.com/library /content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105 –

+0

@ Konyv12 array.remove(at:Index)。提供索引值 –

回答

1

我建议

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

是的,我的坏,修理它,斯里。 – JuicyFruit

+0

我有一个多维数组。这会为此工作吗? – konyv12

+0

@ konyv12还没有尝试过,但由于过滤器得到了一个'closure',理论上它应该如果你改编这个代码而不是使用'CLLocationCoordinate2D'而是'[Class]' – JuicyFruit