'How do I convert an struct of type Data to an object of type CNGroup?

I'm having trouble converting a CNGroup object to a Data object and back to a CNGroup object. I decided to start rethinking the problem again. Somewhere along the way I decided that I should use the Data class to save a CNGroup object to CloudKit. I also learned that the field type to use in my CKRecord object would be of the type bytes.

Am I correct so far?

I am able to convert a CNGroup object to a Data object and back again unless I store the Data object in CloudKit and then retrieve it before I convert the Data object back to a CNGroup object, in which case I get an error when I try to access the pointee property of the typed pointer. That would be an UnsafeBufferPointer, an UnsafeMutableBufferPointer, or an UnsafePointer.

I've tried a lot of different code using different ways. It is impractical to put so much code in my post. I have used the copyBytes method and the withUnsafeBytes method of the Data object.

There is one simple code, and that is when I converted the CNGroup object to a Data object:

func convertCNGroupToData(fromCNGroup group: inout CNGroup) -> Data {

    return Data(bytes: &group, count: MemoryLayout.size(ofValue: group))

}

I am looking for a simple way to do what I want. I am relooking at Apple's documentation of Data and NSData.

I am not able to be more specific with this question. I appreciate any effort to help me with this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source