从核心数据传递值

Passing Values from Core data

我目前有核心数据,我想传递持久存储中存在的选定图钉的坐标。所以我过滤了获取的对象并与视图注释坐标匹配。位置是我的实体的一个实例。我的代码如下:

 var location: Location!
 var latitude: Double = 0.0
 var longitude: Double = 0.0

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        performSegue(withIdentifier: "toPhotos", sender: self)
            let savedPins = fetchResultController.fetchedObjects!
        location = savedPins.filter({[=10=].latitude == view.annotation?.coordinate.latitude && [=10=].longitude == view.annotation?.coordinate.longitude}).first
            self.longitude = location.longitude
            self.latitude = location.latitude
    }

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let vc = segue.destination as? photoAlbumViewController {
            vc.dataController = dataController
            vc.longitude = longitude
            vc.latitude = latitude
}


当它转入时,它不会在第一次尝试时发送坐标,直到我第二次转回 photoalbumviewcontroller,然后我收到第一个坐标。

看起来你在设置变量之前正在继续。更改为:

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        let savedPins = fetchResultController.fetchedObjects!
        location = savedPins.filter({[=10=].latitude == view.annotation?.coordinate.latitude && [=10=].longitude == view.annotation?.coordinate.longitude}).first
        self.longitude = location.longitude
        self.latitude = location.latitude

        //moved to after setting the variables
        performSegue(withIdentifier: "toPhotos", sender: self)

    }