使用核心数据在Swift 3中保存和检索MKAnnotations

时间:2017-03-22 02:34:37

标签: ios core-data swift3 mkannotation

经过详尽的搜索和建立HitList和ToDo应用程序后,我还没有更接近实现核心数据和MKAnnotations的使用。我已经看到了有关使用NSFetchedResults,NSUserDefaults或其他废话的各种建议。我想知道如何实现保存引脚位置,然后从CoreData中检索它。以下是我徒劳的尝试。

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    var locationManager: CLLocationManager!
    var storedLocations: [StoredLocations]! = []

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var markSpot: UIBarButtonItem!
    @IBOutlet weak var segmentedControl: UISegmentedControl!

    // Part of the Fail
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.isToolbarHidden = false

        locationManager = CLLocationManager()
        locationManager.requestWhenInUseAuthorization()

        let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView)
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        toolbarItems = [trackingButton, flexibleSpace, markSpot]

        mapView.delegate = self
        // More Fail
        mapView.addAnnotation(getData() as! MKAnnotation)
    }

        // Miserable Fail.. Again
        func getData() {
            do {
            storedLocations = try context.fetch(StoredLocations.fetchRequest())
            }
            catch {
                print("Fetching Failed")
            }

        }

    @IBAction func markSpotPressed(_ sender: Any) {
        let annotation = MKPointAnnotation()
        let userLatitude = mapView.userLocation.coordinate.latitude
        let userLongitude = mapView.userLocation.coordinate.longitude
        annotation.coordinate = CLLocationCoordinate2D(latitude: userLatitude, longitude: userLongitude)
        annotation.title = "Dropped Pin"
        mapView.addAnnotation(annotation)

    // Another Part of the Fail
        let newPin = StoredLocations(context: context)
        newPin.latitude = userLatitude
        newPin.longitude = userLongitude
        (UIApplication.shared.delegate as! AppDelegate).saveContext()

    }

1 个答案:

答案 0 :(得分:1)

您的getData()函数没有返回任何内容,即使它已经返回,您也不希望它返回MKAnnotation,因为您可能有多个存储对象。您可以让它返回MKAnnotation

的数组
override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.isToolbarHidden = false

    locationManager = CLLocationManager()
    locationManager.requestWhenInUseAuthorization()

    let trackingButton = MKUserTrackingBarButtonItem(mapView: mapView)
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
    toolbarItems = [trackingButton, flexibleSpace, markSpot]

    mapView.delegate = self

    if let annotations = getData() {
        mapView.addAnnotations(annotations)
    }
}

func getData() -> [MKAnnotation]? {

    do {
        storedLocations = try context.fetch(StoredLocations.fetchRequest())
        var annotations = [MKAnnotation]()
        for storedLocation in storedLocations {
            let newAnnotation = MKPointAnnotation()
            newAnnotation.coordinate.latitude = storedLocation.userLatitude
            newAnnotation.coordinate.longitude = storedLocation.userLongitude
            annotations.append(newAnnotation)
        }
        return annotations
    }
    catch {
        print("Fetching Failed")
    }
    return nil
}