I am using Swift for an iOS app.
I need to store a DateComponents object as a field in CloudKit.
How would I do that?
So far I think I need to set the CloudKit field to type Bytes, and convert the DateComponents object to a Data object. Looking at the documentation for the Data class, I can't figure out how to initialize the Data object. I have no idea how to use the UnsafeBufferPointer<SourceType> used in one of the initializers.
The following code gives a runtime error:
newRecord.setObject((dateComponents as! __CKRecordObjCValue), forKey: DatabaseNameStrings.fieldTime)
Here's the runtime error message in the debug window:
Could not cast value of type 'NSDateComponents' (0x1d04208c8) to '__C.CKRecordValue' (0x1d0420ab0).
Hope this is not too late. How about the following code snippet?
let dayToday = DateComponents(weekday: 6) let dayTodayData = try? NSKeyedArchiver.archivedData(withRootObject: dayToday, requiringSecureCoding: true) let newRecord = CKRecord(recordType: "ComponentData") newRecord["data"] = dayTodayData
Try it in a playground. It worked in mine.