Using your entities

As mentioned before, every model or Entity in your Core Data database is represented by an NSManagedObject. There are a couple of ways to create or generate NSManagedObject subclasses. In the simplest of setups, an NSManagedObject subclass contains just the properties for a certain managed object and nothing else. If this is the case, you can let Xcode generate your model classes for you.

This is actually what Xcode does by default. If you build your project now and add the following code to viewDidLoad() in FamilyMembersViewController, your project should compile just fine:

let fam = FamilyMember(entity: FamilyMember.entity(), insertInto: persistentContainer.viewContext)

This works automatically; you don't have to write any code for your models yourself. Don't worry about what the preceding code does just yet, we'll get into that very soon. The point is that you see that a FamilyMember class exists in your project even though you didn't have to create one yourself.

If the default behavior doesn't suit the approach you want in your app  for instance, if you want to prevent your code from modifying your models by defining your variables as private(set)  you may want to create a custom subclass instead of making Xcode generate the classes for you. A custom NSManagedObject subclass for FamilyMember could look like this:

class FamilyMember: NSManagedObject {   
    @NSManaged private(set) var name: String   
    @NSManaged private(set) varfavoriteMovies: [Movie]?   
} 

This custom FamilyMember subclass makes sure that external code can't modify the instances by making the setters on FamilyMember private. Depending on your application, it might be a good idea to implement this since it will ensure that your models can't accidentally change.

One final option you have is to let Xcode generate the properties for your NSManagedObject as an extension on a class you define. This is particularly useful if you have some custom stored properties that you'd like to define on your model or if you have a customized NSManagedObject subclass that you can use as the base for all of your models.

All code that Xcode generates for your Core Data models is added to the Build folder in Xcode's Derived Data. You shouldn't modify it, or access it directly. These files will be automatically regenerated by Xcode whenever you perform a build, so any functionality you add inside the generated files will be overwritten.

For the MustC app, it's okay if Xcode generates the model definition classes since there are no custom properties that you need to add. In the model editor, select each entity and make sure that the Codegen field is set to Class Definition; you can find this field in the Data Model inspector panel:

At this point, you are all set up to store your first data in the Core Data database.

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

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