Creating the models

In order for Core Data to understand which models your application uses, you must define them in Xcode's model editor. Let's create a new model file so you can add your own models to the MustC application. Create a new file, and from the file template selection screen, pick Data Model. Name your model file MustC. First, you will set up the basic models, and then see how you can define a relationship between family members and their favorite movies:

Your project now contains a file called MustC.xcdatamodeld. Open this file to go to the model editor. In the bottom-left corner of the editor, you'll find a button labeled Add Entity. Click this button to add a new Entity and name it FamilyMember.

When you select an Entity by clicking it, you can see all of its attributes, relationships, and fetched properties. Let's add a name property to the 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, you shouldn't care too much about indexing since you're not performing lookups by family members' names, and, even if you 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 you 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 those options in their default setting:

In addition to a FamilyMember Entity, MustC also needs 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 added this property, you can set up a relationship between family members and their favorite movies.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.164.121