코어 데이터에 위치 좌표를 저장하고 싶습니다.이를 저장하고 검색하는 올바른 방법은 무엇입니까? 순간 코어 데이터에 위치 좌표 저장 - 스위프트 3
나는이와 점점 오류를하고있는 중이 야 : 는var newPlaceCoordinate = CLLocationCoordinate2D()
var newPlaceLatitude = CLLocationDegrees()
var newPlaceLongitude = CLLocationDegrees()
나는 자동 완성 위젯을 사용하여 Google지도 API를 사용하는 내 좌표를 얻는다.
let newPlaceCoordinate = place.coordinate
self.newPlaceCoordinate = place.coordinate
let newPlaceLatitude = place.coordinate.latitude
print(newPlaceLatitude)
self.newPlaceLatitude = place.coordinate.latitude
let newPlaceLongitude = place.coordinate.longitude
print(newPlaceLongitude)
self.newPlaceLongitude = place.coordinate.longitude
그리고 내가 사용하는 좌표를 저장 :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newPlace = NSEntityDescription.insertNewObject(forEntityName: "StoredPlace", into: context)
newPlace.setValue(newPlaceCoordinate, forKey: "coordinate")
newPlace.setValue(newPlaceLatitude, forKeyPath: "latitude")
newPlace.setValue(newPlaceLongitude, forKeyPath: "longitude")
그리고 나는 모든 문자열 유형으로 설정 한 속성이 있습니다. 그런 다음 내있는 viewDidLoad에서 검색 할 내가 가진 :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "StoredPlace")
request.returnsObjectsAsFaults = false
if let coordinate = result.value(forKey: "coordinate") as? String
{
let latitude = (coordinate as NSString).doubleValue
let longitude = (coordinate as NSString).doubleValue
let markers = GMSMarker()
markers.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
markers.icon = GMSMarker.markerImage(with: UIColor.yellow)
markers.tracksViewChanges = true
markers.map = vwGMap
}
그래서이 작업과 속성 '의 오류가 생성되지 않습니다 속성 = "좌표"; 원하는 유형 = NSString; 주어진 유형 = NSConcreteValue '. 몇 가지 질문이 있습니다.
어떻게 좌표 데이터를 적절하게 저장합니까? CLLocationDegrees 또는 CLLocationCoordinate2D로 저장합니까? 이 객체를 NSString으로 변환하려면 어떻게해야합니까? 아니면 내 속성에 다른 유형을 사용해야합니까? (Double 또는 Integer로 변경하려고했으나 오류도 발생했습니다.) 핵심 데이터에서 저장하고 검색하고자하는 위치 좌표가 여러 개 있습니다.
감사합니다. 'Appdelegate에 구성원이 없습니다 managedObjectContext'라는 오류가 나타납니다 - 추가해야하는 항목이 있습니까? (Swift 3과 Xcode의 최신 버전을 사용하고 있습니다. 이전 버전에서 이해하면 CoreData 템플릿이 상당히 변경되었습니다 ...) –
배포 대상이 iOS 10 인 경우 appDelegate.persistentContainer.viewContext를 사용해야합니다. – Matt
그래서 시도해 본 결과 여전히 같은 오류가 발생했습니다. Double 속성을 설정하자마자 충돌이 발생합니다. 나는 왜 그것이 작동하지 않을지 모르겠다. 그동안 Long과 Lat를 문자열로 저장하여이 작업을 완료했으며 CoreData에 잘 저장하는 것으로 보입니다.이제 문자열을 CLLocationDegrees로 다시 설정하는 방법을 알아 보려고합니다. 이것이 저장 및 검색을위한 적절한 방법이라고 생각하십니까? –