gpt4 book ai didi

ios - 替换 CloudKit 记录时遇到问题

转载 作者:IT王子 更新时间:2023-10-29 05:31:47 24 4
gpt4 key购买 nike

我在行“if (NSKeyedUnarchiver.unarchiveObject(with: loadedData as! Data) as? CKRecord) !=无”。

还有另一个 fatal error :当我尝试将 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")
}
}

最佳答案

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

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")
}
})
}
}
}

关于ios - 替换 CloudKit 记录时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40274448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com