Swift 2 MKMapViewDelegate rendererForOverlay optionality

2.7k views Asked by At

In Swift 1.2 I have this:

class UVC: NSViewController, MKMapViewDelegate {
    // ...

    // **************************************
    // MARK: MapView Delegate
    // **************************************
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! {
        if overlay is OSGBTiles {
            return OSGBTilesRenderer(tileOverlay: overlay as! OSGBTiles)
        } else if overlay is ESRI {
            return ESRIRenderer(shapeFileOverlay: overlay as! ESRI)
        } else if overlay is MKTileOverlay {
            return MKTileOverlayRenderer(overlay: overlay)
        } else {
            print("Unknown overlay")
        }
        return nil
    }
}

Swift 2 has changed the definition of mapView:rendererForOverlay to now return MKOverlayRenderer rather than MKOverlayRenderer!, and will not now allow me to return nil (unsurprisingly) as MKOverlayRenderer is not NilLiteralConvertible. However, the documentation (in Xcode 7) still says:

Return Value

The renderer to use when presenting the specified overlay on the map. If you return nil, no content is drawn for the specified overlay object.

What am I supposed to return if passed an overlay that I don't recognise?

1

There are 1 answers

5
Mark Knopper On

According to the most recent MKMapView.h, the new function declaration is now:

// Current renderer for overlay; returns nil if the overlay is not shown.
@available(iOS 7.0, *)
func rendererForOverlay(overlay: MKOverlay) -> MKOverlayRenderer?