【问题标题】:Class instance reference from property来自属性的类实例引用
【发布时间】:2017-12-08 22:18:40
【问题描述】:

我有一个带有注释作为属性的类

class UserClass {
   var id : String?
   var annot : MKPointAnnotation?
}

然后我创建了这个类的几个实例,例如:

var user1 = UserClass()

然后在地图上定义、创建和添加每个用户的注释。

稍后在代码中,用户点击注释,我需要对相应用户实例的引用。如何获得?

@objc(mapView:didSelectAnnotationView:) func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annot = view.annotation as? MKPointAnnotation {
        let user = // Need the user instance here to acces user id etc
    }
}

【问题讨论】:

标签: swift class properties


【解决方案1】:

您可能在使用注释填充地图视图的类中的数组中包含所有用户。如果没有,将所有用户存储在一个数组中,然后:

@objc(mapView:didSelectAnnotationView:) func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
   if let annot = view.annotation as? MKPointAnnotation {
      let user = usersArray.first(where:{$0.annot == annot})
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多