1

I'm attempting to programmatically update the subtitle within MKAnnotation. The following lines of code produce a cannot assign subtitle in annotation error:

let annotation: MKAnnotation = mapView.annotations[0] as! MKAnnotation
annotation.subtitle = nil

Could anyone help lead me in a direction where I can update the subtitle after the MKAnnotation is shown within the map?

Mike Walker
  • 2,944
  • 8
  • 30
  • 62

1 Answers1

1

MKAnnotation is a protocol. In that protocol, subtitle is read-only. That means it cannot be set.

So, because you have cast this annotation to an MKAnnotation, you cannot set the subtitle.

If you know know what class the annotation really is, then you can cast to that class, and presumably it will have a subtitle which is read/write so you can set it. For example, it might be an MKPointAnnotation, or you might have your own custom annotation class. Then you'll be able to cast to that, and now you can set the subtitle.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I created my own custom annotation class and when I try to update the subtitle, it doesn't update on the annotation within the map. However if I print the subtitle before and after the change, it appears to update properly. Is there a call that I need to make to update the annotation within the map or should that be handled automatically? – Mike Walker May 06 '15 at 20:49
  • I don't think you can show the change. Just remove the annotation and replace it with one that has the subtitle you want (but the same title and the same coordinate as the one you removed). – matt May 06 '15 at 20:57
  • I think there's a problem that the callout view doesn't detect the change so it doesn't show it. I've seen hacky workarounds like this: http://stackoverflow.com/a/3944235/341994 But I really can't recommend that sort of thing. – matt May 06 '15 at 21:03