简体   繁体   中英

How to get 2 unrelated Swift objects to know how to do the same sets of function

Before anyone says inheritance.. hear me out first.

I have 2 totally unrelated view controllers. They each have an MKMapView . I would like both of them to conform to and implement the same delegate methods.

For example, I want both to implement:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polyline = overlay as! MKPolyline
        let renderer = MKPolylineRenderer(polyline: polyline)
        renderer.strokeColor = UIColor.red
        renderer.lineWidth = 4
        return renderer
    }

Again, these 2 view controllers aren't related at all, so I don't want to make a base class. As a matter of fact, these 2 view controllers are already inheriting from their respective inheritance hierarchy.

Use a protocol and a default implementation.

protocol SomeMapFunctions {
    var mapView : MKMapView? { get }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
}

extension SomeMapFunctions {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polyline = overlay as! MKPolyline
        let renderer = MKPolylineRenderer(polyline: polyline)
        renderer.strokeColor = UIColor.red
        renderer.lineWidth = 4
        return renderer
    }
}

class VC1 : UIViewController, SomeMapFunctions {
    var mapView : MKMapView?
}

class VC2 : UIViewController, SomeMapFunctions {
    var mapView : MKMapView?
}

As shown, any common properties that are necessary for the default implementations can be put into the protocol as well.

One solution could be:

protocol CommonStuff {
   func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer 
}

extension CommonStuff where Self: UIViewController {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let polyline = overlay as! MKPolyline
        let renderer = MKPolylineRenderer(polyline: polyline)
        renderer.strokeColor = UIColor.red
        renderer.lineWidth = 4
        return renderer
    }
}

Then, adopting the protocol by both view controllers would provide them the same behaviour.

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