2017-09-18 10 views
0

長押しのジェスチャーが認識されると、iOS CoreDataにマークされた場所を保存しようとしています。ジェスチャーを呼び出すときはいつでも、アプリは常にクラッシュします。私はまた、これらの2つのcoreData型をDoubleに設定しました。私はまだiOS開発に精通していないので、coreDataに座標をできるだけ簡単に保存するにはどうしたらいいですか?保存方法CoreDataでの位置

@IBAction func saveLocation(_ sender: Any) { 
    guard let longPress = sender as? UILongPressGestureRecognizer else 
    { return } 
    if longPress.state == .ended { 
     let touchLocation = longPress.location(in: mapView) 
     let coordinate = mapView.convert(touchLocation, toCoordinateFrom: mapView) 

     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     let context = appDelegate.persistentContainer.viewContext 
     let newLocation = NSEntityDescription.insertNewObject(forEntityName: "GeoData", into: context) 
     newLocation.setValue(coordinate.latitude as Double, forKey: "myLatitude") 
     newLocation.setValue(coordinate.longitude as Double, forKey: "myLongitude") 
     do 
     { 
      try context.save() 
     } 
     catch 
     { 
     //ERROR 
     } 
    } 

} 
+0

クラッシュログとは何ですか? –

+0

申し訳ありませんが、私はそれがちょうど私のばかげた可能性があると思います。私は気付かなかったブレークポイントに入ったにちがいない。それが正しく保存されているようだ。 – Artvader

答えて

1

私はコアデータに長いタップジェスチャーと保存場所を作成し、長いタップジェスチャーを作成し、this--

のようにコアデータに場所を保存します。

let longGesture = UILongPressGestureRecognizer(target: self, action: 
#selector(longTap(_:))) 
mapView.addGestureRecognizer(longGesture) 

func longTap(_ sender: UIGestureRecognizer){ 
    print("Long tap") 
    if sender.state == .ended { 
     print("UIGestureRecognizerStateEnded") 
     //Do Whatever You want on End of Gesture 
     let touchLocation = sender.location(in: mapView) 
     let coordinate = mapView.convert(touchLocation, 
toCoordinateFrom: mapView) 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     let context = appDelegate.persistentContainer.viewContext 
     let newLocation = 
NSEntityDescription.insertNewObject(forEntityName: "GeoData", into: 
context) 
     newLocation.setValue(coordinate.latitude as Double, forKey: 
"myLatitude") 
     newLocation.setValue(coordinate.longitude as Double, forKey: 
"myLongitude") 
     do 
     { 
      try context.save() 
     } 
     catch 
     { 
      //ERROR 
      print(error) 
     } 
    } 
    else if sender.state == .began { 
     print("UIGestureRecognizerStateBegan.") 
     //Do Whatever You want on Began of Gesture 
    } 
} 
関連する問題