Steve
Steve

Reputation: 1153

Trying to use the original form of data from a CKRecord in Swift

I am trying to convert CKRecords downloaded from cloudkit back to the original form of data they were (in this case a CLLocation). I get the error "Cannot convert value of type 'CKRecord' to expected argument type 'CLLocation'" when I try to call the function on line 17.

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

Upvotes: 1

Views: 656

Answers (2)

matt
matt

Reputation: 535945

Even if you ask for records whose value is a CLLocation, the result of the query is still an array of CKRecord, not an array of CLLocation. If a CKRecord contains a CLLocation, you need to extract it by calling objectForKey: and cast it to a CLLocation.

Upvotes: 1

Pranav Wadhwa
Pranav Wadhwa

Reputation: 7746

Save your record by setting a key of the record to your location:

recordThatYouAreSaving.setObject(yourLocation, forKey: "location")

Then to get it:

addBoundry(records[i]["location"] as! CLLocation)

Upvotes: 2

Related Questions