1

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.

Guy Kahlon
  • 4,510
  • 4
  • 30
  • 41
  • 1
    The word *entity* is the *definition of the data* while a *managed object* hold the actual data. In your code snippet it looks like you have a class called `Entity` but it's not an entity, it's a (managed) object. You're confusing terms. An *entity* is conceptually similar to what a *class* is. – Daniel Eggert Jan 26 '16 at 22:43
  • @DanielEggert thanks, – Guy Kahlon Jan 27 '16 at 08:20

2 Answers2

1

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/

Daniel Eggert
  • 6,665
  • 2
  • 25
  • 41
1

So the answer you received is the correct CoreData method to use properties directly. Your question though, regarding Graph, was not answered correctly with the above answer. The easiest method to accomplish what you like, is to create a wrapper class around the Graph Entity object you are managing.

This will give you all the CoreData features without the work, plus the elegance of using properties directly.

If you are going to use Graph, I would recommend to use it as it is, until you are comfortable. The reason being, is the additional features Graph offers, will be awkwardly available to you unless you know what you are doing, if you start creating wrappers everywhere.

CosmicMind
  • 1,499
  • 1
  • 10
  • 6