CKRecord Gets Key Value

I am trying to get values ​​for a user based on their username. I can get the correct user using:

class func getByUsername(username: String, callback: (records: [CKRecord]) -> ()){
    getSummary(NSPredicate(format: "Username == %@", username)){(records: [CKRecord]) -> Void in
        callback(records: records)
    }
}

class func getSummary(predicate: NSPredicate, callback: (records: [CKRecord]) -> ()){
    let query = CKQuery(recordType: "UserInfo", predicate: predicate)
    let queryOperation = CKQueryOperation(query: query)
    queryOperation.desiredKeys = ["ID"]

    var records = [CKRecord]()
    queryOperation.recordFetchedBlock = {record in records.append(record)}
    queryOperation.queryCompletionBlock = {_ in callback(records: records)}

    CKContainer.defaultContainer().publicCloudDatabase.addOperation(queryOperation)
}

and then I can get the correct entry by calling:

getByUsername("Username"){(records: [CKRecord]) -> Void in
  var record: CKRecord = records[0]
}

and I can also get the correct id using

var id: String = record.recordID.recordName

So, I know that I am retrieving the correct record, but when I try to get the values ​​from the record, for example, the user's email address, it returns nil:

var email: String = record.objectForKey("Email") as String

I also tried:

record.valueForKey("Email")
record.valueForKeyPath("Email")

When I print record.allKeys(), I get an empty array [], and when I print record.allTokens(), I get a refundnil

, , ? , :

CKRecord - -, .

, , , CKRecord iCloud

+4
1

:

queryOperation.desiredKeys = ["ID"]

.

, ID, , ,

var ID = record.recordID.recordName 
+3

Source: https://habr.com/ru/post/1569937/


All Articles