7ball
7ball

Reputation: 2315

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.

Upvotes: 0

Views: 57

Answers (2)

Mick F
Mick F

Reputation: 7439

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.

Upvotes: 0

David Berry
David Berry

Reputation: 41236

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.

Upvotes: 4

Related Questions