ios - 如果 Swift 中存在 NSUserDefault 则尝试更新 CLLocation

标签 ios swift function nsuserdefaults cloudkit

最初,我试图将 CLLocation 保存为 NSUserDefault 值,然后将其存储为 CLoudkit 中的 CKRecord,但出现错误:“defaults.setObject(locationRecord.recordID, forKey: "locationRecordID")”原因是“尝试将非属性列表对象设置为关键 locationRecordID 的 NSUserDefaults/CFPreferences 值”。所以现在我尝试将纬度和经度保存为默认值并替换 Cloudkit 中的旧位置。我目前在“publicDB.fetchRecordWithID((defaults.objectForKey("Location") as!CKRecordID),completionHandler:”行上收到“Thread 1 SIGABRT”错误原因是“无法将类型‘__NSCFDictionary’(0x1a1bec968)的值转换为‘CKRecordID’。

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations.last
        self.loc1 = location!
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.004, longitudeDelta: 0.004))
        self.mapView.setRegion(region, animated: true)
        self.locationManager.stopUpdatingLocation()
        self.locationManager.startMonitoringSignificantLocationChanges()

        self.getRecordToUpdate(locations)

        let lat: CLLocationDegrees = center.latitude
        self.lat1 = lat
        let long: CLLocationDegrees = center.longitude
        self.long1 = long

        locationRecord.setObject(location, forKey: "location")
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        publicData.saveRecord(locationRecord) { record, error in
        }
        if error == nil
        {
            print("Location saved")
        }
    }

func getRecordToUpdate(locations:[CLLocation])
    {
        let defaults = NSUserDefaults.standardUserDefaults()
        var locationRecord:CKRecord

        if defaults.objectForKey("Location") == nil{
            locationRecord = CKRecord(recordType: "location")
            let locationDict = ["lat": lat1, "lng": long1]//
            defaults.setObject(locationDict, forKey: "Location")//
            self.updateLocationRecord(locationRecord, locations: locations)
            print("new")
        }else{
            let publicDB = CKContainer.defaultContainer().publicCloudDatabase
            publicDB.fetchRecordWithID((defaults.objectForKey("Location") as! CKRecordID),completionHandler: {  
                (record, error) in
                if error == nil{
                    self.updateLocationRecord(record!, locations: locations)
                    print("fetched record")
                }else{
                    print("Error fetching previous record")
                }
            })
        }
    }
    func updateLocationRecord(locationRecord:CKRecord, locations:[CLLocation])
    {
        let location = locations.last
        locationRecord.setObject(location, forKey: "location")
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        publicData.saveRecord(locationRecord) { record, error in
        }
        if error == nil
        {
            print("Location saved")
        }
    }

最佳答案

试试这个

您可以将 NSData 保存到 NSUserDefaults。因此,您所需要做的就是将 CLLocation 对象转换为 NSData,如下所示:

// saving your CLLocation object
let locationData = NSKeyedArchiver.archivedDataWithRootObject(your location here)
NSUserDefaults.standardUserDefaults().setObject(locationData, forKey: "locationData")

// loading it
if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("locationData") {
    if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
        println(loadedLocation.coordinate.latitude)
        println(loadedLocation.coordinate.longitude)
    }
}

关于ios - 如果 Swift 中存在 NSUserDefault 则尝试更新 CLLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39604344/

相关文章:

ios - Swift NSPredicate 字符串变量不返回数据

php - 限制变量访问

javascript - 如何将函数参数用作变量的一部分?

python - 我想通过串口将函数发送到 RPi 上的 python

Swift 闭包类型推断

ios - 将事件添加到日历非常慢

ios - 如何拥有相同更新的对象的多个副本,同时在不同 View Controller 中实例化为单独的对象?

php - 从 iOS 发送多部分 POST 并在 PHP $_POST 中读取参数?

ios - 在 FScalendar swift 中禁用 future 日期选择

ios - Realm 数组索引超出范围