从注释中获取数组索引

Get the array index from annotation

您好,我想在用户点击注释时获取索引号。我有以下代码..

class ShopLocation: NSObject, MKAnnotation{

 var identifier = "Shop location"
 var title: String?
 var subtitle: String?
 var coordinate: CLLocationCoordinate2D

    init(name:String,lat:CLLocationDegrees,long:CLLocationDegrees,addInfo:String){
        title = name
        coordinate = CLLocationCoordinate2DMake(lat, long)
        subtitle = addInfo
    }

}
    class LocationList: NSObject {


        var shop = [ShopLocation]()

          override init(){
            shop += [ShopLocation(name:"t1", lat:35.673647,long:139.727686,addInfo:"info")]

            shop += [ShopLocation(name:"t2", lat:35.671928,long:139.760815,addInfo:"info")]

            shop += [ShopLocation(name:"t3", lat:35.713232,long:139.793467,addInfo:"info")]
    }

注释显示在地图上。我想获取索引,例如,如果 t1 被点击,我得到的索引为 1。

不知道写什么

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    LocationList().shop.index(of: )

}

谢谢!

试试下面的代码:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let annotation = view.annotation else {
        return
    }
    for (index, item) in LocationList().shop.enumerated() {
        if let title = view.annotation?.title {
            if item.name! == title! {
                print(index)
                break
            }
        }
    }

我假设 ShopLocation 已经符合 MKAnnotation,如果不符合则将其传递给 MKAnnotationView 的初始化器。这将使它在 delegate 方法中可用,通过 view.annotation

现在您可以访问 ShopLocation 实例,我想到了两种解决方案来找到它在 shop

中的索引
  1. 符合 Equatable - 这将启用 ArrayfirstIndex(of:) 方法(实际上 Collection 符合 Array )

     extension ShopLocation: Equatable {
         static func ==(lhs: ShopLocation, rhs: ShopLocation) -> Bool {
             // place here the conditions for two instances to be equal
         }
    
     // ... later in the code
    
     func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
         guard let shopLocation = view.annotation as? ShopLocation else {
             // this early returns if the annotation is not a shop location,
             // if you don't wan't that you can use an if-let instead
             return
         }
         let index = LocationList().shop.firstIndex(of: shopLocation)
         // do what you need with the index
     }
    
  2. 使用 ArrayfirstIndex(where:) 方法(同样,实际上是 Collection :)

     func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
         guard let shopLocation = view.annotation as? ShopLocation else {
             // this early returns if the annotation is not a shop location,
             // if you don't wan't that you can use an if-let instead
             return
         }
         let index = LocationList().shop.firstIndex(where: { shopLocation in
             return // same conditions as for the Equatable
         })
         // do what you need with the index
     }
    

就我个人而言,我推荐方法 #1,甚至 Apple 也推荐让值类型符合 Equatable。符合 Equatable 有助于减少代码,并启用除此处提到的 index(of:) 之外的许多其他功能。

   func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        var index = shops.index(of: view.annotation! as! ShopLocation)!
    }

帮我完成了工作。