Swift Can';t从mapview获取自定义模型未更改状态函数
我试图在拖动状态完成后打印我的帖子模型,但由于某些原因,它不会打印任何内容。我已经附上了我的模型,它与Swift Can';t从mapview获取自定义模型未更改状态函数,swift,Swift,我试图在拖动状态完成后打印我的帖子模型,但由于某些原因,它不会打印任何内容。我已经附上了我的模型,它与viewFor功能配合得很好。提前谢谢你的帮助 func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
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 {