iphone SDK Development

Similar documents
Copyright 2009 The Pragmatic Programmers, LLC.

Beginning Mac Programming

Cocoa Programming A Quick-Start Guide for Developers

Web Design for Developers A Programmer s Guide to Design Tools and Techniques

Web Design for Developers A Programmer s Guide to Design Tools and Techniques

Practical Programming, Third Edition

Pragmatic Guide to Git

Distributed and Parallel Computing with Ruby

Pragmatic Guide to Sass

SQL Antipatterns. Extracted from: Avoiding the Pitfalls of Database Programming. The Pragmatic Bookshelf

Java by Comparison. Extracted from: Become a Java Craftsman in 70 Examples. The Pragmatic Bookshelf

Programming Clojure. Extracted from: Second Edition. The Pragmatic Bookshelf

Java By Comparison. Extracted from: Become a Java Craftsman in 70 Examples. The Pragmatic Bookshelf

Build Database Apps in Elixir for Scalability and Performance

Reactive Programming with RxJS 5

Agile Web Development with Rails 5.1

Learn Functional Programming with Elixir

Developing Android on Android

ios 9 SDK Development

Reactive Programming with RxJS

Practical Vim, Second Edition

Build Safe and Maintainable Front-End Applications

Agile Web Development with Rails 5

Node.js the Right Way

Automate with Grunt. Extracted from: The Build Tool for JavaScript. The Pragmatic Bookshelf

Copyright 2006The Pragmatic Programmers, LLC.

Build ios Games with Sprite Kit

Node.js 8 the Right Way

Build Reactive Websites with RxJS

Dart for Hipsters. Extracted from: The Pragmatic Bookshelf

Practical Vim, Second Edition

Deploying with JRuby 9k

Practical Programming, 2nd Edition

ios 8 SDK Development

Effective Testing with RSpec 3

Agile Web Development with Rails 5

Complex Network Analysis in Python

Pragmatic Guide to Sass 3

Modern Vim. Extracted from: Craft Your Development Environment with Vim 8 and Neovim. The Pragmatic Bookshelf

Copyright 2009 The Pragmatic Programmers, LLC.

Designed for Use Usable Interfaces for Applications and the Web

Copyright 2010 The Pragmatic Programmers, LLC.

Design It! Extracted from: From Programmer to Software Architect. The Pragmatic Bookshelf

Programming Clojure, Third Edition

Programming Google Glass, Second Edition

Programming Kotlin. Extracted from: Creating Elegant, Expressive, and Performant JVM and Android Applications. The Pragmatic Bookshelf

My First iphone App. 1. Tutorial Overview

1 Build Your First App. The way to get started is to quit talking and begin doing. Walt Disney

The Classbook Web Application

IOS 8 SDK Development: Creating IPhone And IPad Apps With Swift By Chris Adamson

ios 8 SDK Development

Object-Oriented Programming in Objective-C

ITP 342 Mobile App Dev. Interface Builder in Xcode

STARCOUNTER. Technical Overview

Scribe SolutionPak: HubSpot and Dynamics CRM Lead and Contact Synchronization

iphone App Basics iphone and ipod touch Development Fall 2009 Lecture 5

The kits are only available online. Participants will have access to their Course Kit 45 days prior to the course.

CS193P: HelloPoly Walkthrough

Things You Should Know

Docker for Rails Developers

crm-now/ps Outlook Plugin Manual Version 5.0

How to Make a Book Interior File

Mastering Xcode for iphone OS Development Part 1. Todd Fernandez Sr. Manager, IDEs

My First iphone App (for Xcode version 6.4)

Interface-Oriented Design

PHPMYADMIN STARTER BY MARC DELISLE DOWNLOAD EBOOK : PHPMYADMIN STARTER BY MARC DELISLE PDF

New Features in Splashtop Center v An Addendum to the Splashtop Center Administrator s Guide v1.7

opensap TEXT ANALYTICS WITH SAP HANA PLATFORM WEEK 1

Hello! ios Development

Technical White Paper

SecureAssist Rulepack Configurator v User Guide December 2015

XML Primer Plus By Nicholas Chase

Page 1. GUI Programming. Lecture 13: iphone Basics. iphone. iphone

My First Cocoa Program

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

Fast Traffic Sniper... 3 CPV School... 4 Popup Blockers and CPV Ads... 4 Seeing Your Own Popups... 4 How it Works... 5 Keyword Targeting...

Skynax. Remote Assist Console. User Guide

Tutorial 1: Simple Parameterized Mapping

COPYRIGHTED MATERIAL. Installing Xcode. The Xcode Installer

Distinction Communication Website Manual

Learn More: The Digital Bundle

Landing Page Optimization What is Split Testing?... 13

1 Accounts 2. 2 Account 4. 3 Sent and Received Invoices Color Coding Invoice Book (OS X) Job Offers... 9.

Photo Editing Made Easy.

Click EDIT to CHANGE ALBUM SHAPE

ios Core Data Example Application

Technical White Paper

Saving Data in ios Hands-On Challenges

Structuring an App Copyright 2013 Apple Inc. All Rights Reserved.

Mac OS X and ios operating systems. Lab 1 Introduction to Mac OS X and ios app development. Gdańsk 2015 Tomasz Idzi

Customize. Building a Customer Portal Using Business Portal. Microsoft Dynamics GP. White Paper

Introduction... 1 Part I: Getting Started... 7

Marketing Insider... 3 Section 1 Your List... 4 AWeber Basics... 4 Create your Account... 5 Exploring AWeber s Members Area...

MobileFast SyncStudio. A Complete Mobile Database Synchronization Solution. Quick-Start Manual. Release 1.61, May 2014

Managing Devices and Corporate Data on ios

COPYRIGHTED MATERIAL. 1Hello ios! A Suitable Mac. ios Developer Essentials

Scribe Monitor App. Version 1.0

Mastering Clojure Macros

Complex Network Analysis in Python

COGNOS (R) ENTERPRISE BI SERIES COGNOS REPORTNET (TM)

Transcription:

Extracted from: iphone SDK Development Building iphone Applications This PDF file contains pages extracted from iphone SDK Development, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com. Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printer versions; the content is otherwise identical. Copyright 2009 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://www.pragprog.com Copyright 2009 Bill Dudney and Chris Adamson. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-10: 1-934356-25-5 ISBN-13: 978-1-934356-25-8 Printed on acid-free paper. P2.0 printing, December 2009 Version: 2010-2-3

MODELING 224 11.4 Modeling Now that we have seen the way the template has set up Core Data for us, let s start fleshing out our application by building our managed object model. The managed object model is a central part of how Core Data interacts with your model. Core Data uses the entity when it translates between the world of Objective-C and the world of SQLite. The model is similar to the schema of your database with a little bit of extra information about how to translate from Objective-C to SQL and back. Roughly speaking, each entity corresponds to a table in the database, and each attribute corresponds to a column in that table. On the Objective-C side, each entity corresponds to a class, and each attribute corresponds to a property on that class. There is, of course, a ton of detail behind how a MOM is used to map objects into rows, and vice versa, but you don t have to fully understand all that thanks to the great tools available in Xcode. Let s take a quick look at the UI. Double-click Conference.xcdatamodel in Xcode. You should see something that looks more or less like this screenshot:

MODELING 225 We are going to modify this template data model so it captures our Track and Session entities. Roughly speaking, we need to accomplish these tasks: Change the template-provided Event entity to the Track entity. Add the name and trackabstract attributes and the sessions relationship to the Track entity. Create the Session entity. Add the name, sessionabstract, and sessionid attributes and the track relationship to the Session entity. Generate the Track and Session classes and add them to the project. With these two entities and the relationship between them modeled, Core Data will have the information it needs to make them persistent. Start by renaming the Event entity to Track. Select the entity; then in the Entity Attributes inspector, change the name from Event to Track. Also, change the class name from NSManagedObject to Track. Track has two attributes, name and trackabstract. Both of them are strings with no constraints (length, regex, and so on). Add them by selecting the + button under the properties list (the list to the right of the entity list) and choosing Add Attribute. When you have added them, you should have something that looks roughly like this:

MODELING 226 Make sure you turn off Optional for each of the attributes. Although this is not necessary, it will help make sure that no bogus data makes it into our database. As you have probably noticed, there are lots of other ways we can customize the constraints that Core Data will place on our attributes. For example, if we were to provide a regular expression (also known as a regex) in the Attributes inspector, Core Data would ensure that whatever value was placed in that attribute matches the regex before it saved the value. If the value fails any of the constraints, a validation error is raised. We can use the error to create a user visible/understandable message to show so the user knows what to fix. We are not going to take the time to go into detail on that, but the Core Data documentation [App09b] has more information on presenting errors to your users. Now that we have the Track entity, we need to create the Session entity. Under the list of entities (where Track currently shows up on the topleft side), click the + button. Rename the new entity to Session, change its class name to Session, and add three string attributes: sessionid, name, and sessionabstract. Again, all the Session attributes should have no constraints, and the Optional switch should be turned off. When you are done, you should have something that looks like this:

MODELING 227 Now that we have the two entities set up, we need to create the relationship between them. Select the Track entity, and click the + button under the property list (top-left side). Choose Add Relationship, and name the new relationship sessions. Make the relationship s destination the Session entity, choose the to-many checkbox, and make sure the delete rule is Cascade. Next we need to add the inverse relationship to the Session entity. Select the Session entity, add a relationship, name it track, set its destination to Track, and choose its inverse to be sessions. Set the delete rule on the new relationship to Nullify, and turn Optional off. When you are done, the model should look something like this: Our next step is to generate the classes that will be used to represent this model in our application. In Xcode, add a new group called Model Classes, and select it. Go back to the model, select all the entities with D-a, and then choose File > New File from the menu. Select the Managed Object Class from the dialog box that pops up, as in the next screenshot. 4 4. If you don t have the model editor as the front window when you select the File > New File menu item, you won t see the Managed Object Class option in the New File Wizard.

MODELING 228 Click Next on the next page of the wizard. You don t need to change anything here, but make sure the Conference target is selected. On the final page of the wizard, make sure the checkbox next to Session and Track is selected so that the generator uses both our entities when it makes the classes. The page should look like this: After you click the Finish button, you should have a header and implementation file for both the Track and Session classes.

MODELING 229 Let s take a look at the code the generator made for us. Here is the header file for the Session class: Download CoreData/Conference02/ModelClasses/Session.h @class Track; @interface Session : NSManagedObject { } @property (nonatomic, retain) NSString * sessionid; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSString * sessionabstract; @property (nonatomic, retain) Track * track; @end Not much that is unexpected here. The generator created a property for each of the attributes and relationships in the model, all of them set to retain their object values. Next let s look at the implementation. Here is the code from the.m file: Download CoreData/Conference02/ModelClasses/Session.m @implementation Session @dynamic sessionid; @dynamic name; @dynamic sessionabstract; @dynamic track; Line 1 - @end This file has very little substance too. The new and interesting piece is the use of @dynamic for the properties. This declaration says to the compiler that the properties will have get/set method pairs provided for them at runtime, so the compiler does not need to provide them. Core Data provides these methods for us, so we don t have to worry about them. Let s look at the Track class next. Here is the header file: Download CoreData/Conference02/ModelClasses/Track.h @class Session; - @interface Track : NSManagedObject { - } 5 - @property (nonatomic, retain) NSString * trackabstract; - @property (nonatomic, retain) NSString * name;

TRACK TABLE VIEW 230 - @property (nonatomic, retain) NSSet* sessions; - 10 @end - - @interface Track (CoreDataGeneratedAccessors) - - (void)addsessionsobject:(session *)value; - - (void)removesessionsobject:(session *)value; 15 - (void)addsessions:(nsset *)value; - - (void)removesessions:(nsset *)value; - @end The top half is probably what you expected, which is a property for every attribute and relationship in the entity. The CoreDataGeneratedAccessors category might be new, however, so let s look at it in a bit more detail. An Objective-C category is a way for us to add methods to an object. On line 12, the CoreDataGeneratedAccessors category is declared for the Track class. All the methods declared in this category (until the @end) become part of the interface. Now let s look at the implementation: Download CoreData/Conference02/ModelClasses/Track.m @implementation Track @dynamic trackabstract; @dynamic name; @dynamic sessions; @end Not much here... the methods that are declared in the CoreDataGeneratedAccessors category are generated by Core Data at runtime (thus the name of the category). We need only the category in the header file so that we can call the methods without a compiler warning. This little bit of Objective-C magic is brought to you by Key-Value Coding (KVC). The Apple Key-Value Coding [App08e] documentation has a bunch of the detail. For now, though, you can ignore the detail and just know that you can call addsessionsobject: on any instance of Track. Now that we have our model in place, Core Data will be able to persist it. Let s look at the code needed to make the Track table view work. 11.5 Track Table View The good thing is you already know how table views work. They ask their data source for the number of sections, for the number of rows

The Pragmatic Bookshelf The Pragmatic Bookshelf features books written by developers for developers. The titles continue the well-known Pragmatic Programmer style and continue to garner awards and rave reviews. As development gets more and more difficult, the Pragmatic Programmers will be there with more titles and products to help you stay on top of your game. Visit Us Online iphone SDK Development s Home Page http://pragprog.com/titles/amiphd Source code from this book, errata, and other resources. Come give us feedback, too! Register for Updates http://pragprog.com/updates Be notified when updates and new books become available. Join the Community http://pragprog.com/community Read our weblogs, join our online discussions, participate in our mailing list, interact with our wiki, and benefit from the experience of other Pragmatic Programmers. New and Noteworthy http://pragprog.com/news Check out the latest pragmatic developments, new titles and other offerings. Buy the Book If you liked this ebook, perhaps you d like to have a paper copy of the book. It s available for purchase at our store: pragprog.com/titles/amiphd. Contact Us Online Orders: www.pragprog.com/catalog Customer Service: support@pragprog.com Non-English Versions: translations@pragprog.com Pragmatic Teaching: academic@pragprog.com Author Proposals: proposals@pragprog.com Contact us: 1-800-699-PROG (+1 919 847 3884)