Swift无法以MKA注释分配字幕 [英] Swift cannot assign subtitle in MKAnnotation
问题描述
我正在尝试以编程方式更新MKAnnotation
中的字幕.以下代码行产生cannot assign subtitle in annotation
错误:
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
在地图上显示MKAnnotation后,有人可以帮助我指引我更新字幕吗?
Could anyone help lead me in a direction where I can update the subtitle after the MKAnnotation is shown within the map?
推荐答案
MKAnnotation是协议.在该协议中,subtitle
是只读的.这意味着无法设置.
MKAnnotation is a protocol. In that protocol, subtitle
is read-only. That means it cannot be set.
因此,由于您已将此注释转换为MKAnnotation,因此无法设置subtitle
.
So, because you have cast this annotation to an MKAnnotation, you cannot set the subtitle
.
如果您知道真正注释是什么 class ,然后,则可以转换为该 class ,并且大概会有一个subtitle
可以读/写,因此您可以对其进行设置.例如,它可能是MKPointAnnotation,或者您可能有自己的自定义注释类.然后,您可以对此进行强制转换,现在可以设置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
.
这篇关于Swift无法以MKA注释分配字幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!