Search code examples
swiftcloudkitckrecord

Creating a CKRecord in Cloudkit?


I am very new to CloudKit and am trying to create a record to my public database recordType "UserProfile".

I think my syntax is incorrect in the last line of code because I'm getting the error "cannot invoke 'saveRecord' with an argument list of type (CKRecord, (CKRecord!, NSError) -> Void)'

Am I getting an error due to a Swift update that I am unaware of, any help is appreciated!

var nameRecord = CKRecord(recordType: "UserProfile")
    nameRecord.setValue("Jimmy", forKey: "Name")
    CKContainer.defaultContainer().publicCloudDatabase.saveRecord(nameRecord) { (savedRecord: CKRecord!, error: NSError) -> Void in

    }

Solution

  • The error is because you are passing in a closure of type (CKRecord!,NSError) which you should be passing in one of type (CKRecord!,NSError!), or one using explicit optionals rather than implicitly unwrapped optionals.

    Try:

    var nameRecord = CKRecord(recordType: "UserProfile")
        nameRecord.setValue("Jimmy", forKey: "Name")
        CKContainer.defaultContainer().publicCloudDatabase.saveRecord(nameRecord) { (savedRecord: CKRecord?, error: NSError?) -> Void in
    
        }