如何使用查询在swift中获取cloudkit数据

时间:2016-08-17 18:37:45

标签: ios swift cloudkit cllocation ckrecord

我想让所有用户'存储在cloudkit中的位置,然后由每个设备下载。我在storeLocation中的代码上标记了我收到错误:

  

"无法转换类型'(NSError?,[CKRecord]?)' (又名'(可选,可选>)')预期参数类型'(错误:NSError ?,记录:[CKRecord]?) - >空隙'"

//saves location in cloud kit //currently works well:

var locArray = [CKRecord]()
func storeLocation(location:CLLocation) {
        let locationRecord = CKRecord(recordType: "location")
        locationRecord.setObject(location, forKey: "location")
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        publicData.saveRecord(locationRecord) { (records, error) in
            if error != nil {
                print("error saving locations: \(error)")
            } else {
                print("Locations saved: \(records)")
                loadLocation((error, self.locArray)) //where I get error******
            }
        }
    }

//fetches location from cloud kit:

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")
            completion(error: error, records: nil)
        } else {
            print("found locations: \(records)")
            completion(error: nil, records: records)
        }
    }
}

1 个答案:

答案 0 :(得分:2)

我相信而不是:

loadLocation((error, self.locArray))

你需要这样称呼:

loadLocation() { (error, records) in
    // do something here with the returned data.
}