Search code examples
iosswiftcore-datacosmicmind

Is there any way to work with real Object in Graph Library for core data


I've just started to play with library looks very interesting for core data called Graph. I just wondering if there is any way to work with real object and not just with Entity for example:

let elon: Entity = Entity(type: "Person")
elon["firstName"] = "Elon"
elon["lastName"] = "Musk"

I would like to work with Class\Struct called Person with 2 properties: firstName and LastName. I know I can to create a Class \ Struct and create computed property and return the exact value, But I wondering if there is an elegant way to do this without so many boilerplate code.


Solution

  • Create a subclass of NSManagedObject called Person and give it a @NSManaged var firstName: String property. Then in you data model, create an entity called Person and set its class to be Person. Then add the firstname property to it.

    Our book has more details on how to create properties, classes, etc., https://www.objc.io/books/core-data/ -- and the preview covers how to model data: https://www.objc.io/books/core-data/preview/