首页 > 解决方案 > 使用带有自定义参数的协议函数

问题描述

我想使用协议中的mapView函数,MkmapviewDelegate但不是将注释作为MKAnnotation变量,而是希望它来自继承自的自定义类型MKAnnotation

问题是,当我编写时"viewFor annotation: MKAnnotation",使用自定义类中的方法时出现错误,而当我编写"viewFor annotation: MyCustomClass"程序时没有使用此方法并且我没有得到我想要的。

注意:最终目标是为动态创建的注释显示自定义图像。

谢谢您的回答

功能: 功能

标签: swiftxcodemkmapviewprotocols

解决方案


你可以像这样保护你的自定义类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() == ... {}
}

然后你可以再次使用你的自定义类方法


推荐阅读