简体   繁体   中英

Using a protocol func with a custom parameter

I would like to use the mapView function from the MkmapviewDelegate protocol but instead of having annotation being a MKAnnotation variable I want it to be from a custom type that inherits from MKAnnotation .

The thing is when I write "viewFor annotation: MKAnnotation" , I get an error using the methods from my custom class and when I write "viewFor annotation: MyCustomClass" the program doesn't use this method and I don't get what I want.

Note: the final goal is to display custom image for annotations that are created dynamically.

Thank you for your answers

The function: 功能

You could guard your custom class in viewFor annotation: MKAnnotation like:

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() == ... {}
}

and then you can use your custom class methods again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM