Reputation: 171
So when I was using Swift 1.2, this function worked fine and gave me the functionality I needed.
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolyline {
let pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = self.lightBlue
pr.lineWidth = 14
return pr
}
//I get the error here, because I need to return something
}
Now, I get an error saying "Missing return in a function expected to return 'MKOverlayRenderer'".
Is this still how this method is used? What can I do to remedy this?
Upvotes: 1
Views: 1761
Reputation: 311
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer
will only be called for each overlay that you add to your instance of MKMapView.
If you only add an MKPolyline
to your mapView, you can be sure that when this delegate function is called, the overlay
parameter will be of type MKPolyline
. Since this is the case, there is no reason to check if the overlay parameter is an instance of MKPolyline
with if overlay is MKPolyline
. You can just do this:
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = self.lightBlue
pr.lineWidth = 14
return pr
}
If you ever add a different type of overlay to your MKMapView, you can do this:
if overlay is MKPolyline {
// return renderer for MKPolyline overlay
return mkPolylineRenderer
} else {
// handle the different type of overlay...
return otherTypeOfOverlayRenderer
}
The Swift compiler is intelligent enough to infer from the if/else block that something will always be returned.
Upvotes: 4