Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [92]

By Root 960 0
a bad user experience, because a GUI that’s navigable when it’s displaying twenty objects in a table view may become impossible to use effectively when there are thousands of entries.

The solution to this problem involves an important piece of functionality: a way to provide some search criteria so that we can limit the objects pulled out of storage by a controller. In this chapter, we’ll introduce you to the use of NSPredicate to restrict searches to just a subset of all the objects for a given Entity. You’ll see how to specify a “hard-coded” NSPredicate, both in Interface Builder and in your source code, and you’ll also see how to let your users define the values for a predicate using NSPredicateEditor.

Creating QuoteMonger


We’ll demonstrate the use of NSPredicate by creating an app called QuoteMonger, which allows you to keep track of all your favorite shows, and famous quotes from them. This will be a Core Data application with two entities, and a GUI made partly by using the assistant that appears when dragging an entity from Xcode to Interface

Builder, and partly by setting things up all on our own. Figure 9-1 shows you a view of the completed application.

Figure 9-1. QuoteMonger’s data entry and search windows

NOTE: By the time you’ve gotten this far in the book, you should have gained some experience with the basics of creating Xcode projects and files, so we’re not going to continue giving exact click-by-click instructions for some of the things you’ve already done several times, instead reserving the highest level of detail for the new topics we’re covering in each chapter.

Create the Project and Its Data Model


Start by creating a new Core Data project in Xcode, and naming it QuoteMonger. Then edit the QuoteMonger_DataModel.xcdatamodel, and add two entities called Show and Quote.

Give Show a single attribute called name, of type String. Turn off the Optional checkbox, and turn on the Indexed checkbox. Then give Quote two String attributes called quoteText and character. For each of those, turn on the Indexed checkbox, but only turn off the Optional checkbox for quoteText, leaving it on for character (because it’s conceivable that you can’t quite remember or determine who said something, but want to store it as a quote anyway).

Finally, make a one-to-many relationship from Show to Quote (remember, that means making two relationships: a to-many from Show to Quote called quotes, and a to-one from Quote to Show called show, and configuring them to be each other’s inverse). The resulting data model should look like something like Figure 9-2.

Figure 9-2. The data model for QuoteMonger

The Data Entry Window


With the data model in place, it’s time to start building the GUI. The interesting part, which this chapter is mostly about, is the search window. But, we can’t do any searching until we have some data to search, so we’ll start by creating the portion of the GUI that lets us do data entry. We’re going to make a very basic GUI for this, using our old friend, the New Core Data Entity Interface assistant, modifying things just slightly so that shows and quotes are displayed in a single window, and that only quotes that are tied to the currently selected show are displayed.

A Two-Part Autogenerated Window


Double-click MainMenu.xib in your Xcode project to open it in Interface Builder. This brings up the standard empty application nib you’ve learned to deal with before. Select the NSWindow instance in the main nib window, and use the resizing control to make it taller, about twice as tall as it started out. We’ll need this space, because we’re going to put two sets of views in here, one for the Show entity and one for the Quote entity.

Now go back to the data model editor in Xcode, and -drag the Show entity over to the empty window in Interface Builder (don’t forget, you can ⌘Tab to switch back to Interface Builder during the drag). When you release the drag, the New Core Data Entity Interface assistant will appear. Choose Master/Detail View, click to turn on the Add/Remove

Return Main Page Previous Page Next Page

®Online Book Reader