我正在尝试将从cloudkit下载的CKRecords转换回原来的数据形式(在本例中为CLLocation)。当我尝试在第17行调用函数时,我收到错误“无法将类型'CKRecord'的值转换为预期的参数类型'CLLocation'”。
func loadLocation(completion: (error:NSError?, records:[CKRecord]?) -> Void)
{
let query = CKQuery(recordType: "Location", predicate: NSPredicate(value: true))
CKContainer.defaultContainer().publicCloudDatabase.performQuery(query, inZoneWithID: nil){
(records, error) in
if error != nil {
print("error fetching locations: \(error)")
completion(error: error, records: nil)
} else {
print("found locations: \(records)")
completion(error: nil, records: records)
guard let records = records else {
return
}
for(var i = 0; i<records.count; i += 1)
{
addBoundry(records[i])
}
}
}
}
答案 0 :(得分:2)
通过将记录的密钥设置到您的位置来保存记录:
recordThatYouAreSaving.setObject(yourLocation, forKey: "location")
然后得到它:
addBoundry(records[i]["location"] as! CLLocation)
答案 1 :(得分:1)
即使您要求其值为CLLocation的记录,查询的结果仍然是CKRecord数组,而不是CLLocation数组。如果CKRecord包含CLLocation,则需要通过调用objectForKey:
将提取到CLLocation。