无法更换CloudKit记录

Ste*_*eve 11 function nsuserdefaults ios swift cloudkit

我得到致命的错误:"无法在" if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data) as? CKRecord) != nil" 行上将'NSTaggedPointerString'类型的值转换为'NSData' ".

还有另一个致命错误:"当我尝试将locationRecord保存为默认值时,尝试将非属性列表对象设置为键locationData的NSUserDefaults/CFPreferences值".

var locationRecord = CKRecord(recordType: "location")

func getRecordToUpdate(_ locations:CLLocation)
{
    if defaults1.object(forKey: "locationData") == nil{
        locationRecord.setObject(locations, forKey: "location")
        defaults1.set(locationRecord, forKey: "locationData")
        self.updateLocationRecord(locations: locations)
    }else{
        if let loadedData = defaults1.object(forKey: "locationData") {
            print("continue")
            if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data)) != nil
            {
                let publicDB = CKContainer.default().publicCloudDatabase
                publicDB.fetch(withRecordID: locationRecord.recordID,completionHandler: {
                    (record, error) in
                    if error == nil
                    {
                        publicDB.delete(withRecordID: (record?.recordID)!, completionHandler: {
                            (record, error) in
                            if(error == nil){
                                print("old record delete")
                                self.locationRecord.setObject(locations, forKey: "location")
                                self.defaults1.set(self.locationRecord, forKey: "locationData")
                                self.updateLocationRecord(locations: locations)
                            }
                            else{
                            }
                        })
                    }else{
                        print("Error fetching previous record")
                        }
                })
            }
        }
    }
}
    func updateLocationRecord(locations: CLLocation)
    {
        locationRecord.setObject(locations, forKey: "location")
        let publicData = CKContainer.default().publicCloudDatabase
        publicData.save(locationRecord, completionHandler: { record, error in
        })
        if error == nil
        {
            print("Location saved")
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 4

这是最终为我工作的代码:

if let loadedData = defaults1.object(forKey: "locationData") as? Data {
                let litt = NSKeyedUnarchiver.unarchiveObject(with: loadedData) as! CKRecord
                let publicDB = CKContainer.default().publicCloudDatabase
                    publicDB.fetch(withRecordID: litt.recordID ,completionHandler: {
                        (record, error) in
                        if error == nil
                        {
                            publicDB.delete(withRecordID: (record?.recordID)!, completionHandler: {
                                (record, error) in
                                if(error == nil){
                                    print("old record delete")
                                    let id = self.locationRecord.recordID.recordName
                                    self.locationRecord.setObject(locations, forKey: "location")
                                    self.defaults1.set(id, forKey: "locationData")
                                    self.updateLocationRecord(locations: locations)
                                }
                                else{
                                }
                            })
                        }else{
                            print("Error fetching previous record")
                            }
                    })
                }
        }
    }
Run Code Online (Sandbox Code Playgroud)