从核心数据传递值

Passing Values from Core data

提问人:Sola 提问时间:6/19/2020 更新时间:6/19/2020 访问量:42

问:

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

 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({$0.latitude == view.annotation?.coordinate.latitude && $0.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,然后我收到第一个坐标。

iOS Swift iPhone 核心数据

评论


答:

-1赞 nicksarno 6/19/2020 #1

看起来您在设置变量之前正在进行排序。更改为:

 func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        let savedPins = fetchResultController.fetchedObjects!
        location = savedPins.filter({$0.latitude == view.annotation?.coordinate.latitude && $0.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)

    }