Nil is not compatible with the return type "MKOverlayRenderer"

I get this error

"Nil is not compatible with the return type" MKOverlayRenderer " .

Here is my code:

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { if overlay is MKCircle { let circleRenderer = MKCircleRenderer(overlay: overlay) circleRenderer.lineWidth = 1.0 circleRenderer.strokeColor = UIColor.purpleColor() circleRenderer.fillColor = UIColor.purpleColor().colorWithAlphaComponent(0.4) return circleRenderer } return nil } 

Why is this happening?

+6
source share
4 answers

returns MKPolylineRenderer () instead of nil.

+12
source

This seems to be answered:

Swift 2 MKMapViewDelegate rendererForOverlay optionality

You are simply not allowed to return zero. The overlay must have a visualization tool. Replace "return nil" with the statement.

+3
source

You should not return nil for this delegate function. The map displays a render for each of the overlays. In your case, you should do the following:

 func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { assert(overlay is MKCircle, "overlay must be circle") let circleRenderer = MKCircleRenderer(overlay: overlay) circleRenderer.lineWidth = 1.0 circleRenderer.strokeColor = UIColor.purpleColor() circleRenderer.fillColor = UIColor.purpleColor().colorWithAlphaComponent(0.4) return circleRenderer } 

I do not think that you should return MKPolylineRenderer() , because this may hide your error.

+2
source

Because nil is not an MKOverlayRenderer. If you expect the function to return zero, then the return type should be optional. As defined, a function can only return the actual MKOverlayRenderer.

+1
source

All Articles