尝试使用 Swift 中 CKRecord 的原始数据形式
Trying to use the original form of data from a CKRecord in Swift
我正在尝试将从 cloudkit 下载的 CKRecords 转换回它们的原始数据形式(在本例中为 CLLocation)。当我尝试调用第 17 行的函数时出现错误 "Cannot convert value of type 'CKRecord' to expected argument type '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])
}
}
}
}
通过将记录的键设置为您的位置来保存您的记录:
recordThatYouAreSaving.setObject(yourLocation, forKey: "location")
然后获取:
addBoundry(records[i]["location"] as! CLLocation)
即使你查询值为CLLocation的记录,查询的结果仍然是一个CKRecord数组,而不是一个CLLocation数组。如果 CKRecord 包含 CLLocation,您需要通过调用 objectForKey:
和 cast 将其 extract 为 CLLocation。
我正在尝试将从 cloudkit 下载的 CKRecords 转换回它们的原始数据形式(在本例中为 CLLocation)。当我尝试调用第 17 行的函数时出现错误 "Cannot convert value of type 'CKRecord' to expected argument type '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])
}
}
}
}
通过将记录的键设置为您的位置来保存您的记录:
recordThatYouAreSaving.setObject(yourLocation, forKey: "location")
然后获取:
addBoundry(records[i]["location"] as! CLLocation)
即使你查询值为CLLocation的记录,查询的结果仍然是一个CKRecord数组,而不是一个CLLocation数组。如果 CKRecord 包含 CLLocation,您需要通过调用 objectForKey:
和 cast 将其 extract 为 CLLocation。