SwiftUI 1.0: Save custom Datatype in CoreData

Hi, in my current project I want to save a custom data type in core data but I don't know to do it.
The Core Data Entity:
Code Block
extension WorkoutBuy {
@nonobjc public class func fetchRequest() -> NSFetchRequest<WorkoutBuy> {
return NSFetchRequest<WorkoutBuy>(entityName: "WorkoutBuy")
}
@NSManaged public var beschreibung: String?
@NSManaged public var dauerInterval: Int64
@NSManaged public var dauerPause: Int64
@NSManaged public var dauerPauseDurchgang: Int64
@NSManaged public var durchgaenge: Int64
@NSManaged public var id: UUID?
@NSManaged public var intensitaet: String?
@NSManaged public var name: String?
@NSManaged public var price: Double
@NSManaged public var catagory: Catagory? !!!//here is my own data type !!!
}
extension WorkoutBuy : Identifiable {
}


My own data type:
Code Block
struct Catagory: Codable, Identifiable{
let id: Int
let localizedString: String
let engishName: String?
}


When I do it this way I get this error:

Property cannot be declared public because its type uses an internal type

And:

Property cannot be marked @NSManaged because its type cannot be represented in Objective-C

One thing I tried:
When Catagory is a own core data entity the app is starting but when I add an object I got the following error like 10 times in the console:

2020-12-04 21:58:31.586858+0100 IntervallTraining[4869:1923447] [error] error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null) CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x2812c9380> , threw while encoding a value. with userInfo of (null) Ein Core Data-Fehler ist aufgetreten.

Does anyone have a solution? Thank you in advance.
PS: I use the AppDelegate to create the persistentContainer


Answered by OOPer in 650960022
The error messages are showing the fact.

You cannot use Swift struct as a property of Core Data.


Core Data allows us very limited set of data types as property, where Swift struct is not included.

You can change the type of catagory to Data and store the encoded Data of Catagory into it.
And you need to decode it to Catagory when using it.
(You can write a simple extension to work with it.)

If the custom data type was an NSObject descendant, you could use some Transformable feature of Core Data.
But this is not an option when you want to use Swift struct.
Accepted Answer
The error messages are showing the fact.

You cannot use Swift struct as a property of Core Data.


Core Data allows us very limited set of data types as property, where Swift struct is not included.

You can change the type of catagory to Data and store the encoded Data of Catagory into it.
And you need to decode it to Catagory when using it.
(You can write a simple extension to work with it.)

If the custom data type was an NSObject descendant, you could use some Transformable feature of Core Data.
But this is not an option when you want to use Swift struct.
SwiftUI 1.0: Save custom Datatype in CoreData
 
 
Q