Swift自定义地图调用错误

时间:2017-11-02 23:10:36

标签: ios swift mapkit mapkitannotation callout

之前我使用过自定义地图标注,并让它们运行起来。太糟糕了,这不是那些情况,因为我一直试图弄清楚为什么我一直收到这个错误:“无法将'NSKVONotifying_MKPointAnnotation'(0x1c0115960)类型的值转换为'ShopPeer.CustomBusinessPoint'(0x101159c68)。”我在行“let customAnnotation = view.annotation as!CustomBusinessPoint”中得到此错误。我正在使用本教程作为示例:http://sweettutos.com/2016/03/16/how-to-completely-customise-your-map-annotations-callout-views/ ...另外,请查看下图中的编译器,看看有多少视图出现...不确定为什么会有这么多。

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard !(annotation is MKUserLocation) else { return nil }
    let reuseId = "pin"
    if let pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? CustomBusinessCallOutAnnotatiion {
        return pinView
    } else {
        let  pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView.pinTintColor = UIColor.red
        pinView.canShowCallout = false
        //pinView.rightCalloutAccessoryView = UIButton(type: UIButtonType.infoLight)

        return pinView
    }
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if view.annotation is MKUserLocation { return }

    let customAnnotation = view.annotation as! CustomBusinessPoint
    let views = Bundle.main.loadNibNamed("CustomBusinessCallOut", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomBusinessCallOut


    calloutView.businessName.text = customAnnotation.businessName


    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height * -0.0001)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

enter image description here

1 个答案:

答案 0 :(得分:0)

错误显而易见,你与类型不匹配。例如,视图不是自定义的,或者您没有覆盖自定义类中的.annotation字段。你需要找你的班级,以确保视图是你的超类。调试视图是否为自定义。最后,根据您所关注的文章阅读评论 - 在您的具体问题中将有更多人成为可能的支持者