I'm trying to subclass MKPolyline and MKGeodesicPolyline to store their own individual colours (by having the subclass instances return their own MKPolylineRenderer). It works fine for MKPolyline, but the instances of my MKGeodesicPolyline subclass are not subclasses - simply MKGeodesicPolylines. Can anyone explain why? Here's my code...
protocol MapLineProtocol: MKOverlay {
var width: CGFloat { get set }
var colour: UIColor { get set }
}
extension MapLineProtocol {
var renderer: MKPolylineRenderer {
let polylineRenderer = MKPolylineRenderer(overlay: self)
polylineRenderer.strokeColor = self.colour
polylineRenderer.lineWidth = self.width
return polylineRenderer
}
}
class MapLine: MKPolyline, MapLineProtocol {
var width: CGFloat = 3
var colour: UIColor = .blue
convenience init(start: CLLocationCoordinate2D, end: CLLocationCoordinate2D) {
let line = [start, end]
self.init(coordinates: line, count: line.count)
}
}
class MapGeodesic: MKGeodesicPolyline, MapLineProtocol {
var width: CGFloat = 3
var colour: UIColor = .red
convenience init(start: CLLocationCoordinate2D, end: CLLocationCoordinate2D) {
let line = [start, end]
self.init(coordinates: line, count: line.count)
}
}
let mapLine = MapLine(start: loc.coordinate, end: end)
print("Mapline subclass: \(mapLine)") // <Appname.MapLine: xxx>
self.mapView.add(mapLine)
let geoLine = MapGeodesic(start: loc.coordinate, end: end)
print("Geodesic subclass: \(geoLine)") // <MKGeodesicPolyline: xxx> !!!
self.mapView.add(geoLine)
Accessing the .colour property on mapLine is fine (and the renderer works), but accessing .colour on the geoLine causes a run-time exception (and, of course, the renderer doesn't work if you bypass the colour). Can anyone please explain?