swift - 无法从 mapview 中获取自定义模型确实更改了状态函数

标签 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
}
}

最佳答案

我认为有一个不需要的.coordinate

请更改此行:

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

if let annon = view.annotation as? MyAnno {

关于swift - 无法从 mapview 中获取自定义模型确实更改了状态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57720059/

相关文章:

ios - 为什么UILabel TapGesture 只能在标签初始化后工作?

ios - 使用 Swift iOS 8/9 以编程方式挂断/断开连接/结束通话

ios - 即使重新启动应用程序,也会在标签中保存数字

swift - 不识别符合协议(protocol)的结构数组

swift - 如何使用 Swift 以编程方式跨 iOS 导航 Controller 进行排序?

ios - masksToBounds 与 clipsToBounds

可以抛出和不能抛出的方法中的 Swift 类型推断

ios - 当按下按钮触发时,UILabels 不会立即更新

xcode - 使用 Cocoapods 导入 SwiftyJSON 时出错

ios - Swift + 宏参数