0

I have searched now all over the internet and I have tried almost everything. My current project is:

I read the current coordinates from the User Location and save the latitude and longitude in CoreData and the name of this place(user can give a name)

 // MARK: - AddPlace Button Events

@IBAction func onAddButton(sender: AnyObject) {

        //searching the current location
        let locCoord = CLLocationCoordinate2DMake(self.locationManager.location!.coordinate.latitude, self.locationManager.location!.coordinate.longitude)

        // 1. pass the data from the current location to lat and lng
        let lat = locCoord.latitude
        let lng = locCoord.longitude

        let ed = NSEntityDescription.entityForName("Store", inManagedObjectContext: moContext)
        let store = Store(entity: ed!, insertIntoManagedObjectContext: moContext)

        // 2. give the passed data to the array from CoreData
        store.storeName = noteTextField.text
        store.storeLng = lng
        store.storeLat = lat



        do  {
            // 3. save the informations in CoreData
            try moContext.save()
            noteTextField.text  = ""
            lat
            lng

            // 4. testing if the coordinates are saved - it works!
            print(store.storeLat)
            print(store.storeLng)

Now my problem is to display a pin on the saved place in another ViewController with a mapView. I tried:

UPDATE:

storeLat --> stored as Double

storeLng --> stored as Double

storeName --> stored as String

//MARK: - Second ViewController with a mapView



    override func viewDidLoad() {
      super.viewDidLoad()

    // Initialize Fetch Request
    let fetchRequest = NSFetchRequest()

    // Create Entity Description
    let entityDescription = NSEntityDescription.entityForName("Store", inManagedObjectContext: self.moContext)

    // Configure Fetch Request
    fetchRequest.entity = entityDescription
    do {
        stores = try moContext.executeFetchRequest(fetchRequest) as! [Store]
        if stores.count > 0
        {

            print("Test")
            // add the annotation
            let annotation = MKPointAnnotation()
            annotation.coordinate = CLLocationCoordinate2D(latitude: store!.storeLat as! Double, longitude: store!.storeLng as! Double)
            annotation.title = store?.storeName
            self.mapView.addAnnotation(annotation)

        } else
        {
            let alert = SCLAlertView()
            alert.showError("Keine Places", subTitle: "Du hast noch keine Places gespeichert.")
        }
    } catch {
        fatalError("Failed to fetch Places: \(error)")
    }

Now it's going into the if - Statement. But I get a failure when I want to place the annotation. fatal error: unexpectedly found nil while unwrapping an Optional value. I think I have forget something to declare. Can someone help? Thank you already!

UPDATE 2

I found this fantastic site. But I have still some problems.

Community
  • 1
  • 1
M.Wordman
  • 1
  • 2
  • Are coordinates correct inside `if stores.count > 0` block? – tbilopavlovic Jul 06 '16 at 12:19
  • @user1941284 I checked it. No, even if there is a saved coordinate in the stores, it's not going into the if - event. Thank your for this idea! – M.Wordman Jul 06 '16 at 12:48
  • How do you populate `stores` in the second VC? – pbasdf Jul 06 '16 at 20:12
  • @pbasdf like this: var stores = [Store]() – M.Wordman Jul 07 '16 at 08:54
  • So you populate `stores` by fetching - but I can't see any code to pull the individual `store` from the array `stores`. My guess is that `store` is consequently nil. Did you want either to pull the first object (`store = stores[0]`) or to iterate through all the objects in the array and display all the annotations? – pbasdf Jul 07 '16 at 22:56
  • @pbasdf So I want to display all the annotations at once. But for understanding and learning it would be nice to also know how does it work to pull the individual store from the array stores, Thanks for your help! – M.Wordman Jul 08 '16 at 07:50
  • I recommend you read up on *arrays* - see the [Swift Manual](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html) - and *for-in loops* - [here](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID120). – pbasdf Jul 08 '16 at 19:59

0 Answers0