swift - 使用带有自定义参数的协议函数
问题描述
我想使用协议中的mapView
函数,MkmapviewDelegate
但不是将注释作为MKAnnotation
变量,而是希望它来自继承自的自定义类型MKAnnotation
。
问题是,当我编写时"viewFor annotation: MKAnnotation"
,使用自定义类中的方法时出现错误,而当我编写"viewFor annotation: MyCustomClass"
程序时没有使用此方法并且我没有得到我想要的。
注意:最终目标是为动态创建的注释显示自定义图像。
谢谢您的回答
功能:
解决方案
你可以像这样保护你的自定义类viewFor annotation: MKAnnotation
:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotation? {
// we check if the annotation is of type MYCLASS, otherwise we make an early exit with nil
guard let myClass = annotation as? MYCLASS else {
return nil
}
if myClass.getState() == ... {}
}
然后你可以再次使用你的自定义类方法
推荐阅读
- bash - 如何验证功能分支不包含任何合并提交并且是最新的?
- python - 如何将 sqlite 数据库添加到 Apache Superset?
- sql - 从 SQL 中的 Varchar 写一个条件语句
- python - ImportError:无法导入名称“MasterServer”
- mysql - 两个关系表,同一个引用的两个外键。如何加入数据?
- reactjs - 反应不受控制的输入:更好地理解为什么它不会在值变化时重新渲染
- html - 如何去除图像周围的白色间隙(引导卡)
- intellij-idea - IntelliJ IDEA:Spring Boot 与 JAR 应用程序配置
- reactjs - 测试 SVG 格式的图标
- amazon-web-services - 使用 boto3 检查云端预签名 URL 是否已过期