13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

230CHAPTER 8: Data Management with Core DataCreating an Object ModelSince Core Data is all about persistence of an object graph, it seems that a good place to startwould be to define one. You’ll create a simple Contacts model where you will be able to storepeople’s names, phone numbers, e-mail addresses, and mailing addresses 3 .You’ll start by creating a new Cocoa Application with Xcode. Ensure that “Use Core Data” ischecked and “Document-Based Application” is not checked. Once the project has been created,select the data model file in the project navigator—it will be named .xcdatamodel,where is the name of your project, with any spaces replaced by underscores. Youwill be presented with an empty model editor in table mode. Here your entities are shown in asource list to the left, and the attributes, relationships, and fetched properties (more on theselater) for the selected entity appear in a table filling the majority of the editor. Below this area,at the bottom of the window, are a few buttons. The leftmost toggles the Entity list betweenshowing a flat list in alphabetical order and a hierarchical list showing the inheritance tree. Nextis a button to add a new entity—if you click and hold here, you can also add new fetch requestsand configurations.To the right hand side of the editor, along the bottom, are two more buttons: a popup button thatallows you to add new attributes, relationships, or fetched properties to a selected entity; and atoggle that will switch the editor’s view between table and graph mode. Graph mode allows youto lay out your data model visually on simulated graph paper, with entities connected by differentgraph lines indicating inheritance and relationships, including the direction and type of eachrelationship (to-one or to-many).How might you model this data? Well, clearly your central entity is a Person. A person has a firstand last name, and has a variety of addresses, whether e-mail or surface mail. They also haveone or more phone numbers. A phone number is a number (most likely formatted), and an e-mailaddress is similarly a string conforming to a certain format. A mailing address has several fields:one or two lines for the house and street, a city/town, county/state, a postal code, and a country.You can model a person fairly simply by placing all these attributes into a single entity. In theeditor for your data model, click the Add Entity button located toward the bottom of the window,and a new entity will be created for you, with its name highlighted ready to accept its new name.Type Person and press Enter.Tip To easily change any entity name or attribute name, double-click on the name in the modeleditor. This works in both Table and Graph mode.In the topmost table on the right side of the editor (the Attributes table), click the + button to adda new attribute and name it firstName. From the Type column, pull down the menu and selectthe String type.3You will be expanding this model in the next chapter to encompass everything within the systemContacts application.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!