无法从MapView获取自定义模型确实更改了状态功能

时间:2019-08-30 03:16:01

标签: swift

我试图在拖动状态结束后打印出我的帖子模型,但是由于某种原因,它什么也不会打印出来。我已经附加了可以与viewFor函数一起正常工作的模型。感谢您的任何提前帮助。

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {

    if newState == MKAnnotationViewDragState.ending {
        if let annon = view.annotation?.coordinate as? MyAnno {

            print(annon.post?.id) //Not working

            view.dragState = MKAnnotationViewDragState.none
        }
    }
}

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var annotationView = MKMarkerAnnotationView()
    guard let annotation = annotation as? MyAnno else {return nil}
    var identifier = ""
    if let dequedView = mapView.dequeueReusableAnnotationView(
        withIdentifier: identifier)
        as? MKMarkerAnnotationView {
        annotationView = dequedView
    } else {
        annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}

型号:

class MyAnno: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var post: Post?
var title: String?

init(post: Post, title: String?, subtitle: String?, latitude:CLLocationDegrees, longitude:CLLocationDegrees) {
    self.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    self.title = title
}
}

1 个答案:

答案 0 :(得分:0)

我认为这里不需要.coordinate

请更改此行:

if let annon = view.annotation?.coordinate as? MyAnno {

if let annon = view.annotation as? MyAnno {