Saving a CKRecordID to CloudKit

537 views Asked by At

I have a CKRecordID that I need to save to CloudKit as a reference. However when I try to save it, I get the following Error:

Cannot convert value of type 'CKRecordID?' to expected argument type 'CKRecordValue?'

Declaration of currentlySelectedUser:

var currentlySelectedUser: CKRecordID?

Retrieval of currentlySelectedUser in another CKQuery:

self.currentlySelectedUser = record.creatorUserRecordID

Saving of currentlySelectedUser:

myRecord.setObject(currentlySelectedUser, forKey: "toUser")

The error occurs at the 3rd line of code.

How do I save this CKRecordID as a reference in another CloudKit record?

1

There are 1 answers

0
Yann Bodson On

You are trying to add a CKRecordID which is not a data type supported by CKRecord. As the error says, it doesn't conform to the CKRecordValue protocol.

Create a CKReference and add it like this:

myRecord["toUser"] = CKReference(recordID: currentlySelectedUser!, action: .None)