Reputation: 471
I created a library in Xcode8 with a method used to add a map to MapKitView and I import that library to a sample project and I called the method in the library then I get an error called "Could not cast value of type 'MKMapView' (0xdc7a48) to 'MKOverlay' (0xdcca0c). (lldb) "
the code in library
import Foundation
import MapKit
public class mapLib: NSObject{
public class func createMap(mapView: MKMapView) ->MKMapView{
let mapView = mapView
//custom map URL
let template = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
let overlay = MKTileOverlay(urlTemplate: template)
overlay.canReplaceMapContent = true
mapView.add(overlay, level: .aboveLabels)
return mapView;
}
}
the code used in the sample app
import UIKit
import MapKit
import mapLib
class ViewController: UIViewController {
@IBOutlet weak var mapV: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
let view = mapLib.createMap(mapView: mapV)
mapV.add(view as! MKOverlay) /////// Thread 1: signal SIGABRT
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have commented the error in sample app code
Upvotes: 1
Views: 382
Reputation: 54775
MKOverlay
is a protocol that MKMapView
doesn't conform to by default, hence the error. Adding an MKMapView
object to another MKMapView
as an overlay simply cannot work, since MKMapView
isn't just a simple overlay object.
If what you actually need is to add all overlays of one MKMapView
to the other, you need to use below code:
let view = mapLib.createMap(mapView: mapV)
mapV.addOverlays(view.overlays)
Upvotes: 1