从注解中获取数组索引

小月

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

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已经符合MKAnnotation,如果没有,则使其符合并将其传递给MKAnnotationView的初始值设定项。这将使其在delegate方法中可用,通过view.annotation

现在您可以访问该ShopLocation实例,我想到了两种解决方案,用于在shop

  1. 顺应Equatable-这将使firstIndex(of:)的方法Array(实际上是CollectionArray符合)

     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. 使用firstIndex(where:)的方法Array试(实际上的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:)这里提到的许多其他功能

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从 Ruby 中的排序中获取数组索引

来自分类Dev

获取angularjs中数组的父索引

来自分类Dev

在numpy数组中获取特定值的索引

来自分类常见问题

在MongoDB中按索引获取数组项

来自分类Dev

获取PHP中复杂数组的索引

来自分类Dev

从PHP中的数组获取值的索引

来自分类Dev

从数组获取索引并在Java中输出

来自分类Dev

如何获取数组中项目的索引?

来自分类Dev

从变量的索引获取numpy数组中的值

来自分类Dev

MongoDB获取数组中当前元素的索引

来自分类Dev

在Vue.js中从数组获取索引

来自分类Dev

在MongoDB中按索引获取数组项

来自分类Dev

根据索引列表从数组中获取元素

来自分类Dev

如何获取数组中的对象索引

来自分类Dev

foreach获取PHP中的数组索引结果

来自分类Dev

如何获取此数组中对象的索引?

来自分类Dev

在数组中查找属性并获取索引

来自分类Dev

获取数组中字符串的索引

来自分类Dev

如何从数组中快速获取对象索引

来自分类Dev

如何获取数组中索引的总和

来自分类Dev

如何获取JSON数组中对象的索引?

来自分类Dev

从索引获取双数组中的值。迅速

来自分类Dev

获取位数组中 1 的索引?

来自分类Dev

单击 Jquery 从数组中获取元素的索引

来自分类Dev

Javascript - 获取布尔数组中真值的索引

来自分类Dev

Laravel,从会话中获取特定的数组索引

来自分类Dev

如何从输入中获取@name 的数组索引?

来自分类Dev

根据包含索引的数组从数组中获取元素

来自分类Dev

如何从PHP中的多维数组获取父数组索引