2017-04-02 65 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