我有一个用于存储注释的数组。只有在您选择注释时,注释才会添加到数组中。添加后,如何检查数组中是否已有注释?我不想要任何重复 - 如果我选择一个已经在阵列中的引脚,它不应该再次添加。
这是我的代码:
var pins = [[String : Double]]()
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if editAndDoneButtonText.title == "Edit" {
println("Pin Selected")
var latFromPin = view.annotation.coordinate.latitude
var lonFromPin = view.annotation.coordinate.longitude
var selectedCoordinatePoint = ["latitude": latFromPin, "longitude": lonFromPin]
var pinCoordinatePoint = ["latitude": latFromPin, "longitude": lonFromPin]
pins.append(selectedCoordinatePoint)
mapView.deselectAnnotation(view.annotation, animated: true)
}
// Remove pins from map by tapping on it
if editAndDoneButtonText.title == "Done" {
var annotationToRemove = view.annotation
mapView.removeAnnotation(annotationToRemove)
println("remove pin - didSelectAnnotationView")
}
}
我在这里阅读其他答案并尝试过但没有成功。这不起作用,因为它允许重复。如果我在地图上选择多个引脚,它们会多次添加到数组中:
if pins.isEmpty == true {
pins.append(selectedCoordinatePoint)
} else if pins.isEmpty == false {
for pin in pins {
if pin == selectedCoordinatePoint {
println("ALREADY INSIDE ARRAY")
} else if pin != selectedCoordinatePoint {
pins.append(selectedCoordinatePoint)
println("ADDED TO ARRAY")
}
}
}
我也试过使用扩展程序:
extension Array {
func contains<T where T : Equatable>(obj: T) -> Bool {
return self.filter({$0 as? T == obj}).count > 0
}
}
任何人都可以帮忙吗?
答案 0 :(得分:1)
使用纬度和经度值作为Dictionary
的关键字:
var pins = [String: [String : Double]]()
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if editAndDoneButtonText.title == "Edit" {
println("Pin Selected")
var latFromPin = view.annotation.coordinate.latitude
var lonFromPin = view.annotation.coordinate.longitude
var selectedCoordinatePoint = ["latitude": latFromPin, "longitude": lonFromPin]
var pinCoordinatePoint = ["latitude": latFromPin, "longitude": lonFromPin]
// old pins.append(selectedCoordinatePoint)
pins["\(latFromPin)_\(lonFromPin)"] = selectedCoordinatePoint
mapView.deselectAnnotation(view.annotation, animated: true)
}
每个密钥只能存在一次。
所以你可以像以后一样检索它:
for key in pins.keys {
let selectedCoordinatePoint = pins[key]!
// go wild with it
}
我会建议你创建一个具有纬度和经度属性的简单Struct
,而不是像这样使用字典数组。一个例子:
struct MySimpleCoordinate: Hashable {
var hashValue: Int {
get {
return "\(latitude.hashValue),\(longitude.hashValue)".hashValue
}
}
let latitude: Double
let longitude: Double
init(latitude: Double, longitude: Double)
{
self.latitude = latitude
self.longitude = longitude
}
}
// must be declared in the global scope! and not just in the class scope
func ==(lhs: MySimpleCoordinate, rhs: MySimpleCoordinate) -> Bool
{
return lhs.hashValue == rhs.hashValue
}
现在让我们用新代码更新该委托:
var pins = Set<MySimpleCoordinate>()
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
if editAndDoneButtonText.title == "Edit" {
println("Pin Selected")
var latFromPin = view.annotation.coordinate.latitude
var lonFromPin = view.annotation.coordinate.longitude
let mySimpleCoordinate = MySimpleCoordinate(latitude: latFromPin, longitude: lonFromPin)
var pinCoordinatePoint = ["latitude": latFromPin, "longitude": lonFromPin]
// old pins.append(selectedCoordinatePoint)
// pins["\(latFromPin)_\(lonFromPin)"] = selectedCoordinatePoint
// there will be no duplicates in a Set
pins.insert(mySimpleCoordinate)
mapView.deselectAnnotation(view.annotation, animated: true)
}
要检索所有值,您可以这样做:
for coord in pins {
println(coord.latitude)
println(coord.longitude)
}