Creating the models

In order for Core Data to understand which models your application uses, you define them in Xcode's model editor. Let's create a new model file so we can add our own models to the FamilyMovies application. Create a new file, and from the file template selection screen pick Data Model. Name your model file FamilyMovies. First, we'll create the basic models and then see how we can define a relationship between family members and their favorite movies. Finally, we'll have a look at NSManagedObject subclasses:

Your project now contains a file called FamilyMovies.xcdatamodeld. Open this file to go to the model editor. In the bottom-right corner of the editor, you'll find a button labeled Add Entity; clicking on this will create our first model, FamilyMember. After doing this, a new entity is added to the list of entities. Rename this new entity to FamilyMember and select it.

After selecting an entity, you can see all of its attributes, relationships, and fetched properties. Let's add a name property to our family member. Click on the plus (+) icon at the bottom of the empty attributes list and add a new attribute called name. Make sure that you select String as the type for this attribute:

Click on this new property to select it. In the sidebar on the right, select the third tab to open the Data Model inspector. This is where you can see more detailed information on this attribute. For instance, you can configure a property to be indexed for faster lookups. You can also choose whether you want the attribute to be optional. For now, we don't care too much about indexing since we're not performing lookups by family members' names and even if we were a family doesn't tend to have hundreds or thousands of members. By default, the Optional checkbox is checked. Make sure that you uncheck this box because we don't want to store family members without a name.

Some other options you have for attributes are adding validation, adding a default value, and enabling indexing in Spotlight. For now, leave all options:

We also need to have a Movie entity. Create this entity using the same steps as before and give it a single property: title. This property should be a string and it shouldn't be optional. Once you've done this, we can set up a relationship between our family members and their favorite movies.