Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [61]

By Root 896 0
Core Data application (not unlike RaiseMan) that has no code.

NSManagedObjectModel


In order to know how to save and load the data in your objects, the system needs to know something about that data: What are the names of the attributes of your object? What are their types? To supply this information, you will create a managed object model. Xcode has an editor that will make it easy for you to describe your data-bearing objects in a .xcdatamodeld file. At runtime, this file will be read in, and an instance of NSManagedObjectModel will be created.

The model uses terminology that is a little different from what we are used to. Instead of “class,” the model uses the term entity. Instead of “instance variable,” the model uses the word property.

In the model are two kinds of properties: attributes and relationships. An attribute holds a simple data type: a string, a date, or a number. We will talk about relationships in Chapter 32.

The RaiseMan application used a subclass of NSDocument named RMDocument. In this application, you will have a subclass of NSPersistentDocument called MyDocument. NSPersistentDocument, a subclass of NSDocument, automatically reads in the model and creates an NSManagedObjectContext. NSPersistentDocument will eliminate the need for many lines of code.

Start Xcode and create a new project of type Cocoa Application. Name the project CarLot, set the Class Prefix to My, and the Document Extension to carlot. Enable both Create Document-Based Application and Use Core Data. Imagine that you own several used-car lots. This application will enable you to keep track of the cars that you wish to sell. When the application is done, it will look like Figure 11.1.

Figure 11.1. Completed Application

In the new project, open MyDocument.xcdatamodeld. Click the Add Entity button at the bottom left of the editor to create a new entity. Name the entity Car.

With the Car entity still selected, click the button at the bottom of the Attributes editor. Add six attributes and give them the following names and types:

Figure 11.2 shows what car looks like in the modeler. We could put a lot of other things in the model, but that is enough for this exercise.

Figure 11.2. Completed Model

Interface


Open MyDocument.xib. Delete the text field that says Your document contents here. Drag an array controller onto the Interface Builder dock. The array controller will be using the document object’s NSManagedObjectContext to fetch and store data. Use the Bindings Inspector to bind the array controller’s managedObjectContext to the File’s Owner’s managedObjectContext (Figure 11.3).

Figure 11.3. Give the Array Controller a Managed Object Context

With the array controller still selected, in the Attributes Inspector under the Object Controller section, set Mode to Entity Name and the entity name to Car. Also, turn on the Prepares Content option, so that the array controller will fetch immediately after it is created.

Each object in the Interface Builder editor can have a label. With the NSArrayController still selected, change to the Identity Inspector and set its Label to Cars. Once you have several array controllers in a XIB, the labels will eliminate a lot of confusion (Figure 11.4).

Figure 11.4. Inspect the Attributes of the Cars Array Controller

View-Based Table Views


In Mac OS X 10.7, Apple introduced view-based table views, which are similar to iOS table views. Prior to 10.7, Cocoa developers used cell-based table views, which are very fast and lightweight, but customization tends to be very involved. View-based table views, on the other hand, make customizing the appearance of your table view cells rather simple: You can use Xcode’s Interface Builder for this purpose.

Drag out a table view (from Cocoa->Data Views). In the Attributes Inspector, set the Content Mode to be View Based and give it three columns. Name the columns Make/Model, Price, and Special.

Use the object hierarchy view (expanded dock view) to select the Table Cell View in the first column and delete

Return Main Page Previous Page Next Page

®Online Book Reader