@DTS Engineer could you please add CloudKit tag to this post, I am unable to add it, thanks!
My Solution
I have managed to get it working on the app target with ValueTransformer and NSSecureCoding but kind of seems odd to store just a normal struct using so much boilerplate code.
Way to reproduce this
The issue is not reproducible on SwiftData only.
Only when you have SwiftData + CloudKit this occurs.
CloudKit minimum reproducible code often requires turning on project setup with capabilities etc.
Problem
On the app target things work ok but for test cases there is a crash as it can't convert from Data to ColorRepresentation.
I have the same file added to 2 different targets, not sure if it has anything with the names and module names because the error seems to suggest it.
Error Domain=NSCocoaErrorDomain Code=4864 "value for key 'root' was of unexpected class 'ColorRepresentation' (0x10326c828) [/Users/homefolder/Library/Developer/CoreSimulator/Devices/E365C6A9-B19A-4A53-8885-05E9DE4D8B84/data/Containers/Bundle/Application/BE80DEBA-5FAF-496A-99CC-E60A8C72A096/WidgetStylesDemo.app].
Allowed classes are:
{(
"'ColorRepresentation' (0x103d98520) [/Users/homefolder/Library/Developer/CoreSimulator/Devices/E365C6A9-B19A-4A53-8885-05E9DE4D8B84/data/Containers/Bundle/Application/BE80DEBA-5FAF-496A-99CC-E60A8C72A096/WidgetStylesDemo.app/PlugIns/WidgetStylesDemoTests.xctest]"
)}" UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'ColorRepresentation' (0x10326c828) [/Users/homefolder/Library/Developer/CoreSimulator/Devices/E365C6A9-B19A-4A53-8885-05E9DE4D8B84/data/Containers/Bundle/Application/BE80DEBA-5FAF-496A-99CC-E60A8C72A096/WidgetStylesDemo.app].
Allowed classes are:
{(
"'ColorRepresentation' (0x103d98520) [/Users/homefolder/Library/Developer/CoreSimulator/Devices/E365C6A9-B19A-4A53-8885-05E9DE4D8B84/data/Containers/Bundle/Application/BE80DEBA-5FAF-496A-99CC-E60A8C72A096/WidgetStylesDemo.app/PlugIns/WidgetStylesDemoTests.xctest]"
)}}
Documentation / Videos:
I wish there are some documentation around SwiftData + CloudKit custom types.