2016-12-30 2 views
0

코어 데이터에 위치 좌표를 저장하고 싶습니다.이를 저장하고 검색하는 올바른 방법은 무엇입니까? 순간 코어 데이터에 위치 좌표 저장 - 스위프트 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로 변경하려고했으나 오류도 발생했습니다.) 핵심 데이터에서 저장하고 검색하고자하는 위치 좌표가 여러 개 있습니다.

답변

0

많은 양의 데이터를 처리하려는 경우 CoreData가 최선의 방법입니다.

이들은 내 성질이 StorePlace이며 위도와 경도 만 저장하고 구현시 필요할 때 좌표를 만드는 것이 가장 좋습니다. 나는 위도, 당신은 아마도 예를 공유 할 수 있습니다

let newItem = getCoordinateFromCoreData() // your retrieval function 
let coordinate = CLLocationCoordinate2D(latitude: newItem.latitude, longitude: newItem.longitude) 
+0

감사합니다. 'Appdelegate에 구성원이 없습니다 managedObjectContext'라는 오류가 나타납니다 - 추가해야하는 항목이 있습니까? (Swift 3과 Xcode의 최신 버전을 사용하고 있습니다. 이전 버전에서 이해하면 CoreData 템플릿이 상당히 변경되었습니다 ...) –

+0

배포 대상이 iOS 10 인 경우 appDelegate.persistentContainer.viewContext를 사용해야합니다. – Matt

+0

그래서 시도해 본 결과 여전히 같은 오류가 발생했습니다. Double 속성을 설정하자마자 충돌이 발생합니다. 나는 왜 그것이 작동하지 않을지 모르겠다. 그동안 Long과 Lat를 문자열로 저장하여이 작업을 완료했으며 CoreData에 잘 저장하는 것으로 보입니다.이제 문자열을 CLLocationDegrees로 다시 설정하는 방법을 알아 보려고합니다. 이것이 저장 및 검색을위한 적절한 방법이라고 생각하십니까? –

0

좌표를 double/NSdata 유형으로 변환 한 다음 CoreData에 저장해야합니다. NSKeyedArchiver 및 NSKeyedUnarchiver를 사용하여 값을 저장하고 검색하십시오. CoreData는 CLLocation 객체를 직접 저장할 수 없습니다.

// Convert CLLocation object to Data 
let newPlaceLatitudeArchived = NSKeyedArchiver.archivedDataWithRootObject(newPlaceLatitude) 
//CoreData Piece 
newPlace.setValue(newPlaceLatitudeArchived, forKeyPath: "latitude") 

// fetch the latitude from CoreData as Archive object and pass it to unarchive to get the CLLocation 

let newPlaceLatitudeUnArchived = NSKeyedUnarchiver.unarchiveObjectWithData(archivedLocation) as? CLLocation 

이 경우 CoreData에서 속성을 이진 유형으로 변환해야합니다.

+0

로 구현 좌표를 사용할 수있는 경도를 검색 한 후

class func insert(latitude: Double, longitude: Double) { let appDelegate = UIApplication.shared.delegate as! AppDelegate let managedObjectContext = appDelegate.managedObjectContext let entity = NSEntityDescription.entity(forEntityName: "StoredPlace", in:managedObjectContext) let newItem = StoredPlace(entity: entity!, insertInto: managedObjectContext) newItem.newPlaceLatitude = latitude newItem.newPlaceLongitude = longitude do { try managedObjectContext.save() } catch { print(error) } } 

을 할 것입니다

@NSManaged public var newPlaceLatitude: Double @NSManaged public var newPlaceLongitude: Double 

그리고 새로운 데이터를 삽입, 그게 어떻게 이루어 졌는가? 난 그냥 모양을 가지고 objC 예제가 올라오고있다. –

관련 문제