Swift 2 MKMapViewDelegate rendererForOverlay optionality

和自甴很熟 提交于 2019-12-01 15:59:10

问题


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:


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?


来源:https://stackoverflow.com/questions/30750560/swift-2-mkmapviewdelegate-rendererforoverlay-optionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!