Getting started with core-data

Creating Your First Model

  • Select the .xcdatamodeld file. You will notice you have no entities. You will have to create one yourself. At the bottom of Xcode you will notice a button that says “Add Entity” click it and you will have a new entity for you to work with on the project.

Data Model

  • In this step there are a few points to mention. First is that you changed your entity name here the entity is given the name Person. Under the attributes section you add the attributes to your model. This button is a plus located at the bottom of the section. You can add any attributes that are relevant to your app. For example if you are making a contact book app, you don’t need to make the model of a Person contain an attribute of Arms with type Boolean. You should keep it related to your app. For example adding a telephone attribute with type Integer or if you like String. You have several options of types to choose from ranging from Booleans to Dates and more.

Creating your model

  • After you are satisfied with your model, you can create the NSManagedObject subclass.

Create NSManagedObjectSubclass

Selecting Data Model

Selecting Entity to manage

  • Here we can see two important things. First, that Person (same as your entity name) is a subclass of NSManagedObject. This is important for your Core Data Stack. Second, you have create two files, a class Person.swift and an extension of Person called Person+CoreDataProperites.swift.

  • All additions should be done in Person.swift, since if you ever change your model and re-run the class generator, it will overwrite everything in Person+CoreDataProperties.swift.



Creating the project