1

What would be an appropriate command for removing formattedCoordinate from the array? I know a removeObject exists for Objective-C, but how about 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
}
5
  • Stick an at: in front of formattedCoordinate in your line with remove Commented Apr 2, 2017 at 20:02
  • See the swift language guide section on collections: developer.apple.com/library/content/documentation/Swift/… Commented Apr 2, 2017 at 20:02
  • @Konyv12 array.remove(at: Index).provide the index value Commented Apr 2, 2017 at 20:04
  • With the dupe target in mind, the only remaining problem is that CLLocationCoordinate2D doesn't (for whatever reason) conform to Equatable – doing so is fairly trivial, see for example stackoverflow.com/a/27364437/2976878 Commented Apr 2, 2017 at 20:14
  • But the user might click on marker that was drawn previously, so it's not always the last member of the array that needs to be removed. Commented Apr 2, 2017 at 20:22

1 Answer 1

1

I would suggest

self.markersArray = self.markersArray.filter({ !(($0.latitude == formattedCoordinate.latitude) && ($0.longitude == formattedCoordinate.longitude)) })
Sign up to request clarification or add additional context in comments.

4 Comments

yeah, my bad, fixed it, sry.
I have a multi-dimensional array. Will this work for that?
@konyv12 haven't tried, but since filter gets a closure, theoretically it should, if you adapt this code to works not with CLLocationCoordinate2D but [Class]
what do you mean exactly by [Class]?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.