Online Book Reader

Home Category

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

By Root 904 0
used in Cocoa. Fortunately, Core Data’s Transformable type provides a simple way to store an image anyway. Create a new attribute, and name it “depiction.” Then turn the Optional checkbox on (and the others off), and choose Transformable from the Type popup list. The view changes to show the configuration for the Transformable type: a single text field labeled Value Transformer Name. The idea is that a Transformable attribute holds a chunk of data that Core Data doesn’t really understand; when Core Data reads this chunk of data from storage, it puts it into an NSData object (an object that can hang onto any old chunk of data, acting as an Objective-C “wrapper” for it), which it then passes through a transformer, a special class that knows how to take an object of one kind and transform it into something else. In the other direction, when an object is going to be saved to storage, Core Data takes the new value and passes it through the same transformer, however this time doing the transformation in the reverse direction.

In this case, we’re going to use a transformer called NSKeyedUnarchiveFromData, which knows how to produce an object of any kind, given an NSData object containing a key-archived version of the object. That surely leaves you wondering what keyed archiving is. We won’t go into it in detail here, but basically keyed archiving is a way of archiving or serializing all of an object’s instance variables in a dictionary-like format, making it possible to reconstruct the object later. This technology is used in a variety of ways in Cocoa, and all of Cocoa’s classes have this functionality built in. This means that you can take an NSImage, or an instance of any other Cocoa class, and stuff it into an NSData object using NSKeyedUnarchiveFromData’s reverse transformation. And if you implement the NSCoding protocol in your own classes, saving and loading their instance variables in a keyed fashion, you can archive your own objects in the same way.

Getting back to the depiction field, the idea is to write the name of the transformer class into the Value Transformer Name text field. As it turns out, keyed archiving is so widespread that in this situation, specifying a transformer for an attribute in Xcode’s modeling tool, it’s used as a default. If you just leave the field blank, the entity will be configured to use NSKeyedUnarchiveFromData to transform model attribute values to and from NSData for storage.

You’ve now defined the entire model for this chapter’s MythBase application, so let’s move on to creating the GUI.

The Automatic GUI


The developer tools have special support for Core Data, allowing you to quickly make a feature-rich GUI application for accessing your model objects through Core Data. This auto-generated GUI will let you create new objects, edit them, search for them, and delete them. And not only can you get that far without writing any code, you can even get a rough version of the GUI up and running with little more than a drag of the mouse. The GUI created by this process works entirely through Cocoa Bindings, using an NSArrayController just like you used for creating DungeonThing in Chapter 6. The key difference is that the NSArrayController created here will access its data through Core Data instead of through your own controller’s array (like we used in DungeonThing). In this section we’ll show you how to do this, and then in the next section we’ll show you how to tweak your GUI to make it even better.

Start by going back to Xcode’s navigation pane. Go into the Resources group and double-click MainMenu.xib to open it in Interface Builder. This brings up a nib file much like you’ve seen in earlier, including a menu and an empty window. Bring the empty window to the front (double-click on its icon in the main window if the window isn’t showing), and make it a bit bigger; somewhere about 500 × 600 will do nicely.

Now go back to Xcode, and bring up your model file if it isn’t still showing. What you’re going to do is -drag the MythicalPerson entity from the graph paper workspace over to

Return Main Page Previous Page Next Page

®Online Book Reader