【问题标题】:Get the array index from annotation从注解中获取数组索引
【发布时间】:2018-08-18 23:35:31
【问题描述】:

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

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: )

}

谢谢!

【问题讨论】:

  • 您需要索引还是需要访问映射到注解视图的ShopLocation 实例?
  • 感谢您的评论。我需要商店数组的索引。

标签: ios swift mapkit mapkitannotation


【解决方案1】:

试试下面的代码:

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 在item.name! == title!没有会员名称
  • @Cristik 使用可选绑定:)
【解决方案2】:

我假设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:) 之外的许多其他功能。

【讨论】:

    【解决方案3】:
       func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
            var index = shops.index(of: view.annotation! as! ShopLocation)!
        }
    

    为我完成了这项工作。

    【讨论】:

    • 请注意,您在这里进行了很多强制转换,如果以后条件发生变化,这可能会使应用程序崩溃。我建议改用if-letguard-let 构造。
    猜你喜欢
    • 2020-06-19
    • 1970-01-01
    • 2016-02-03
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-14
    • 2019-07-30
    • 1970-01-01
    相关资源
    最近更新 更多