Swift cannot assign subtitle in MKAnnotation

三世轮回 提交于 2020-01-06 03:41:04

问题


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?


回答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.



来源:https://stackoverflow.com/questions/30086886/swift-cannot-assign-subtitle-in-mkannotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!