-1

I have a class with an annotation as a property

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

Then I make several instances of this class like:

var user1 = UserClass()

and then define, create and add each user's annotation on the map.

Later in the code user taps on an annotation and I need a reference to the corresponding user instance. How do I get it?

@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
    }
}
3
  • How do you know that a tap on an annotation corresponds to a certain user? Commented Dec 8, 2017 at 22:20
  • because i am creating one annotation for each user Commented Dec 8, 2017 at 22:21
  • Possible duplicate of Find Object with Property in Array Commented Dec 8, 2017 at 22:29

1 Answer 1

0

You probably have all the users in an array in the class you are populating the map view with the annotations. If not, have all the users stored in an array and then:

@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})
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.