2017-03-22 120 views
0

經過對HitList和ToDo應用程序的詳盡搜索和構建之後,我還沒有接近實施Core Data和MKAnnotations的使用。我已經看到使用NSFetchedResults,NSUserDefaults或其他廢話的各種建議。我想知道如何實現保存引腳位置,然後從CoreData中檢索它。以下是我徒勞的嘗試。使用核心數據在Swift 3中保存和檢索MKAnnotations

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() 

    } 
+0

陣列代碼不從工作看太遠;你需要遍歷返回到'storedLocations'的對象,並使用緯度和經度來重新創建註釋對象。 – Paulw11

+0

@ Paulw11那麼知道我整整一天的半安慰並不是完全浪費閱讀和構建演示。如果我不太遠,我會選擇睡覺,或者有人會過來幫助它。我的大腦被炸。 – Chilly

回答

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 
} 
+0

解決了它。對storedLocations中使用的其中一個變量稍作更改。感謝您的幫助,這個問題沒有出現在SO上,問題沒有得到解答。現在我可以在這個週末刪除。 – Chilly