【发布时间】:2017-04-02 19:58:55
【问题描述】:
从数组中删除 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
}
【问题讨论】:
-
在你的行中的 formattedCoordinate 前面加上一个 at: 并删除
-
查看关于集合的 swift 语言指南部分:developer.apple.com/library/content/documentation/Swift/…
-
@Konyv12 array.remove(at:Index).提供索引值
-
考虑到欺骗目标,剩下的唯一问题是
CLLocationCoordinate2D不(无论出于何种原因)符合Equatable- 这样做相当简单,例如参见stackoverflow.com/a/27364437/2976878 -
但是用户可能会点击之前绘制的标记,因此它并不总是需要删除的数组的最后一个成员。
标签: swift cllocationcoordinate2d