ebook img

Learn Cocoa on the Mac PDF

388 Pages·2010·5.022 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn Cocoa on the Mac

Learn Cocoa on the Mac ■ ■ ■ Jack Nutting, Dave Mark and Jeff LaMarche i Learn Cocoa on the Mac Copyright © 2010 by Jack Nutting, David Mark and Jeff LaMarche All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-1859-3 ISBN-13 (electronic): 978-1-4302-1860-9 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editors: Clay Andres, Brian MacDonald, Matthew Moodie, Douglas Pundick Technical Reviewer: Mark Dalrymple Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Tracy Brown Collins Compositor: MacPS, LLC Indexer: John Collin Artist: April Milne Cover Designer: Anna Ishchenko Photo Credit: Weronica Meijer Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- [email protected], or visit http://www.springeronline.com. For information on translations, please e-mail [email protected], or visit http://www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at http://www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. ii Contents at a Glance ■Contents at a Glance.......................................................................................iv ■Contents..........................................................................................................v ■Foreword........................................................................................................xi ■About the Authors.........................................................................................xii ■About the Technical Reviewer......................................................................xiii ■Acknowledgments.........................................................................................xiv ■Preface...........................................................................................................xv ■Chapter 1: Must Love Cocoa............................................................................1(cid:2) ■Chapter 2: Hello, World....................................................................................5(cid:2) ■Chapter 3: Lights, Camera… Actions! (and Outlets, Too)..............................29(cid:2) ■Chapter 4: GUI Components...........................................................................55(cid:2) ■Chapter 5: Using Table Views........................................................................91(cid:2) ■Chapter 6: Cocoa Bindings..........................................................................105(cid:2) ■Chapter 7: Core Data Basics........................................................................133(cid:2) ■Chapter 8: Core Data Relationships.............................................................165(cid:2) ■Chapter 9: Search and Retrieve Core Data with Criteria.............................183(cid:2) ■Chapter 10: Windows and Menus and Sheets.............................................197(cid:2) ■Chapter 11: Document-Based Applications.................................................221(cid:2) ■Chapter 12: Exceptions, Signals, Errors, and Debugging............................239(cid:2) ■Chapter 13: Drawing in Cocoa.....................................................................259(cid:2) ■Chapter 14: Advanced Drawing Topics.......................................................279(cid:2) ■Chapter 15: Working with Files...................................................................303(cid:2) ■Chapter 16: Concurrency.............................................................................315(cid:2) ■Chapter 17: Future Paths.............................................................................335(cid:2) ■Index............................................................................................................347 iv Contents ■Contents at a Glance.......................................................................................iv(cid:2) ■Contents..........................................................................................................v(cid:2) ■Foreword........................................................................................................xi ■About the Authors.........................................................................................xii(cid:2) ■About the Technical Reviewer......................................................................xiii(cid:2) ■Acknowledgments.........................................................................................xiv(cid:2) ■Preface...........................................................................................................xv(cid:2) ■Chapter 1: Must Love Cocoa............................................................................1(cid:2) (cid:2) Get a Mac and Download the Tools........................................................................................................................2 (cid:2) Download the Source Code.....................................................................................................................................3 (cid:2) Getting Help............................................................................................................................................................3 (cid:2) What You Need to Know Before You Begin.............................................................................................................3 (cid:2) Are You Ready?.......................................................................................................................................................4 ■Chapter 2: Hello, World....................................................................................5(cid:2) Building “Hello, World”...........................................................................................................................................5 (cid:2) Exploring the Nib File..............................................................................................................................................9 (cid:2) The Library............................................................................................................................................................10 (cid:2) Dragging Out a Label............................................................................................................................................12 (cid:2) Using the Blue Guidelines.....................................................................................................................................13 (cid:2) The Inspector Window..........................................................................................................................................14 (cid:2) The Attributes Inspector.......................................................................................................................................15 (cid:2) Change the Label’s Color and Font.......................................................................................................................17 (cid:2) Using the Menu Editor...........................................................................................................................................19 (cid:2) Creating Your Application Icon..............................................................................................................................21 (cid:2) Adding Your Icon to Your Project..........................................................................................................................22 (cid:2) Property Lists........................................................................................................................................................25 (cid:2) Running Your Application......................................................................................................................................25 v ■ CONTENTS Sharing Your Creation With the World..................................................................................................................26 (cid:2) Goodbye, Hello World............................................................................................................................................28 ■Chapter 3: Lights, Camera… Actions! (and Outlets, Too)..............................29(cid:2) (cid:2) This Chapter’s Application....................................................................................................................................30 (cid:2) Frameworks, Frameworks Everywhere................................................................................................................30 (cid:2) The Foundation Framework.............................................................................................................................31 (cid:2) The AppKit Framework....................................................................................................................................31 (cid:2) The Cocoa Way: Model-View-Controller...............................................................................................................32 (cid:2) Outlets, Actions, and Controllers..........................................................................................................................33 (cid:2) Declaring Outlets.............................................................................................................................................33 (cid:2) Declaring Actions.............................................................................................................................................34 (cid:2) Outlets and Actions in Action................................................................................................................................35 (cid:2) Enabling Garbage Collection............................................................................................................................35 (cid:2) Creating Our Controller Class...........................................................................................................................37 (cid:2) Declaring an Action and Outlet........................................................................................................................38 (cid:2) Back to Interface Builder.................................................................................................................................39 (cid:2) Setting Up the Window....................................................................................................................................42 (cid:2) Designing the Window’s Interface...................................................................................................................46 (cid:2) Connecting the Outlet......................................................................................................................................48 (cid:2) Connecting the Buttons to Our Action..............................................................................................................49 (cid:2) Implementing the Action Method..........................................................................................................................50 (cid:2) The Application Delegate......................................................................................................................................51 (cid:2) Configuring the Application to Quit on Window Close......................................................................................52 (cid:2) Using the Documentation Browser..................................................................................................................53 (cid:2) Bring It on Home...................................................................................................................................................54 ■Chapter 4: GUI Components...........................................................................55(cid:2) (cid:2) Creating the VillainTracker Application.................................................................................................................57 (cid:2) Creating the VillainTrackerAppDelegate Class.................................................................................................58 (cid:2) Planning for the GUI.........................................................................................................................................60 (cid:2) Building Your Interface.........................................................................................................................................62 (cid:2) Bringing Out Your Text Fields..........................................................................................................................62 (cid:2) Letting them Pick Dates...................................................................................................................................64 (cid:2) Creating the Combo Box..................................................................................................................................65 (cid:2) Indicating a Rating with a Level Indicator........................................................................................................66 (cid:2) Adding Radio Buttons in a Matrix....................................................................................................................67 (cid:2) Adding an Image View.....................................................................................................................................70 (cid:2) Adding Checkboxes in a Matrix.......................................................................................................................71 (cid:2) Configuring a Popup Button.............................................................................................................................72 (cid:2) Inserting a Text View.......................................................................................................................................73 (cid:2) Making Logical Groupings...............................................................................................................................73 (cid:2) Resizing...........................................................................................................................................................76 (cid:2) Time for the Controller.....................................................................................................................................78 (cid:2) Making All the Connections.............................................................................................................................78 (cid:2) Getting Started with Coding..................................................................................................................................80 (cid:2) Standardizing Key Names................................................................................................................................80 (cid:2) Creating the Default Villain..............................................................................................................................81 (cid:2) Paying Attention to Detail................................................................................................................................82 vi ■ CONTENTS (cid:2) Responding to Input.........................................................................................................................................87 (cid:2) In Conclusion........................................................................................................................................................90 ■Chapter 5: Using Table Views........................................................................91(cid:2) (cid:2) Preparing AppController for Multiple Villains........................................................................................................91 (cid:2) Making Way for the Table View............................................................................................................................93 (cid:2) Tweaking the Autosizing Characteristics..............................................................................................................96 (cid:2) Making Some New Connections...........................................................................................................................98 (cid:2) Making Way for the Table View: Code Edition......................................................................................................99 (cid:2) The Table View Needs Your Help........................................................................................................................100 (cid:2) Adding and Deleting Villains...............................................................................................................................102 (cid:2) In Conclusion......................................................................................................................................................104 ■Chapter 6: Cocoa Bindings..........................................................................105(cid:2) (cid:2) Binding to Simple Controls.................................................................................................................................106 (cid:2) Create the DungeonThing Project..................................................................................................................106 (cid:2) Create a Preferences Window.......................................................................................................................107 (cid:2) Binding to NSUserDefaultsController.............................................................................................................111 (cid:2) Create the Main Window................................................................................................................................114 (cid:2) Set Up the DungeonThingAppDelegate..........................................................................................................116 (cid:2) Define Your Constants...................................................................................................................................116 (cid:2) Specify Default Preferences Values...............................................................................................................117 (cid:2) Create the Action Methods............................................................................................................................118 (cid:2) Binding to a Table View......................................................................................................................................120 (cid:2) Make the Code Bindings-Ready.....................................................................................................................121 (cid:2) Show History in Tables..................................................................................................................................124 (cid:2) Create and Configure an Array Controller......................................................................................................126 (cid:2) Bind Table Display via the Array Controller...................................................................................................128 (cid:2) Bind a Text Field via the Array Controller’s Selection....................................................................................128 (cid:2) Making Sure it Works.....................................................................................................................................129 (cid:2) Rinse, Repeat, Rinse, Repeat.........................................................................................................................129 (cid:2) Okay, But How Did That Work?...........................................................................................................................129 (cid:2) Key-Value Coding...........................................................................................................................................129 (cid:2) Key-Value Observing......................................................................................................................................131 (cid:2) Cocoa Bindings: How It Works.......................................................................................................................131 (cid:2) In Conclusion......................................................................................................................................................132 ■Chapter 7: Core Data Basics........................................................................133(cid:2) (cid:2) What You’ve Been Missing.................................................................................................................................133 (cid:2) Creating MythBase..............................................................................................................................................135 (cid:2) Defining the Model.........................................................................................................................................136 (cid:2) The Automatic GUI.........................................................................................................................................142 (cid:2) Refining the GUI.............................................................................................................................................145 (cid:2) Exploring the Template Code..............................................................................................................................150 (cid:2) The App Delegate Interface...........................................................................................................................150 (cid:2) The App Delegate Implementation.................................................................................................................152 (cid:2) Adding Business Logic........................................................................................................................................159 (cid:2) Validating Single Attributes...........................................................................................................................160 (cid:2) Validating Multiple Attributes.........................................................................................................................160 (cid:2) Creating a Custom Attribute...........................................................................................................................163 vii ■ CONTENTS (cid:2) In Conclusion......................................................................................................................................................164 ■Chapter 8: Core Data Relationships.............................................................165(cid:2) (cid:2) Modeling New Entities and Relationships...........................................................................................................166 (cid:2) Model Versioning and Migrations..................................................................................................................167 (cid:2) Preparing for Multiple Model Versions...........................................................................................................167 (cid:2) Adding New Entities.......................................................................................................................................167 (cid:2) Add Relationships..........................................................................................................................................168 (cid:2) Creating a Simple Migration..........................................................................................................................170 (cid:2) Time to Run....................................................................................................................................................172 (cid:2) Updating the GUI.................................................................................................................................................173 (cid:2) Create the Band Window...............................................................................................................................173 (cid:2) Giving Useful Names to Array Controllers......................................................................................................174 (cid:2) Putting People in Bands.................................................................................................................................175 (cid:2) Showing the Bands’ Members.......................................................................................................................177 (cid:2) Create a Venue Window.................................................................................................................................179 (cid:2) Adding a Gig List to the Band Window...........................................................................................................179 (cid:2) Wrapping Up Relationships.................................................................................................................................181 ■Chapter 9: Search and Retrieve Core Data with Criteria.............................183(cid:2) (cid:2) Creating QuoteMonger........................................................................................................................................183 (cid:2) Create the Project and Its Data Model...........................................................................................................184 (cid:2) The Data Entry Window..................................................................................................................................185 (cid:2) Creating the Quote Finder Window................................................................................................................188 (cid:2) Limiting Results with NSPredicate......................................................................................................................189 (cid:2) Creating Predicates........................................................................................................................................189 (cid:2) Specifying an NSAppController’s Predicate in Interface Builder...................................................................191 (cid:2) User-Defined Predicates.....................................................................................................................................191 (cid:2) Adding a Predicate to the App Delegate........................................................................................................192 (cid:2) Add a Predicate Editor to the Search Window...............................................................................................193 (cid:2) Configuring the Predicate Editor....................................................................................................................193 (cid:2) Saving a Predicate..............................................................................................................................................195 (cid:2) In Conclusion......................................................................................................................................................196 ■Chapter 10: Windows and Menus and Sheets.............................................197(cid:2) (cid:2) NSWindow and NSPanel.....................................................................................................................................197 (cid:2) Standard System Panels................................................................................................................................201 (cid:2) A Controller With a Nib of Its Own..................................................................................................................205 (cid:2) Modal Windows.............................................................................................................................................209 (cid:2) System Menus....................................................................................................................................................211 (cid:2) Standard Application Menu Items..................................................................................................................212 (cid:2) Your Own Menus............................................................................................................................................212 (cid:2) Sheets.................................................................................................................................................................218 (cid:2) Wrap-up..............................................................................................................................................................220 ■Chapter 11: Document-Based Applications.................................................221(cid:2) (cid:2) Creating the ColorMix Application.......................................................................................................................222 (cid:2) Examining the Default Nib Files.....................................................................................................................223 (cid:2) Defining the Model.........................................................................................................................................223 (cid:2) Setting Two Colors..............................................................................................................................................224 (cid:2) The Simplest of GUIs......................................................................................................................................225 viii ■ CONTENTS (cid:2) Creating a Default ColorSet............................................................................................................................226 (cid:2) Settling on a File Format.....................................................................................................................................227 (cid:2) Adding Color.......................................................................................................................................................227 (cid:2) The ColorBlendView Class.............................................................................................................................228 (cid:2) Adding Blended Colors to the GUI..................................................................................................................230 (cid:2) Adding Some Background Bling....................................................................................................................234 (cid:2) About Undo and Redo.........................................................................................................................................236 (cid:2) The Undo Stack..............................................................................................................................................236 (cid:2) In Conclusion......................................................................................................................................................237 ■Chapter 12: Exceptions, Signals, Errors, and Debugging............................239(cid:2) (cid:2) Exception Handling.............................................................................................................................................239 (cid:2) Catching Exceptions......................................................................................................................................240 (cid:2) The Limited Role of Exceptions in Cocoa.......................................................................................................241 (cid:2) Create a Test-bed..........................................................................................................................................241 (cid:2) NSInvalidArgumentException.........................................................................................................................246 (cid:2) NSRangeException.........................................................................................................................................249 (cid:2) And the Rest..................................................................................................................................................250 (cid:2) Worse than Exceptions: Death by Signal............................................................................................................251 (cid:2) NSError...............................................................................................................................................................253 (cid:2) Domains and Codes.......................................................................................................................................253 (cid:2) Realizing You Have an Error...........................................................................................................................254 (cid:2) Presenting an Error........................................................................................................................................258 (cid:2) In Conclusion......................................................................................................................................................258 ■Chapter 13: Drawing in Cocoa.....................................................................259(cid:2) (cid:2) Fundamentals.....................................................................................................................................................259 (cid:2) The View Coordinate System.........................................................................................................................260 (cid:2) Frame Rectangle vs. Bounds Rectangle........................................................................................................260 (cid:2) Rects, Points, and Sizes.................................................................................................................................261 (cid:2) Path Basics....................................................................................................................................................261 (cid:2) Creating an NSView Subclass.............................................................................................................................262 (cid:2) Some C Structures.........................................................................................................................................262 (cid:2) The Basic Drawing Method, drawRect:..........................................................................................................263 (cid:2) Graphics States..............................................................................................................................................263 (cid:2) Path Helpers..................................................................................................................................................263 (cid:2) Colors and the Graphics Context....................................................................................................................264 (cid:2) Beyond Color..................................................................................................................................................265 (cid:2) Manual Path Construction..............................................................................................................................266 (cid:2) Pushing Boundaries.......................................................................................................................................266 (cid:2) LOLmaker............................................................................................................................................................269 (cid:2) First Steps to LOL...........................................................................................................................................269 (cid:2) LOLView.........................................................................................................................................................271 (cid:2) Printing Basics....................................................................................................................................................277 (cid:2) Wrapping Up.......................................................................................................................................................278 ■Chapter 14: Advanced Drawing Topics.......................................................279(cid:2) (cid:2) Editing a Curve....................................................................................................................................................279 (cid:2) Preparations...................................................................................................................................................280 (cid:2) Bezier Plumbing.............................................................................................................................................282 ix ■ CONTENTS (cid:2) Drawing a Curve............................................................................................................................................283 (cid:2) Watching the Mouse......................................................................................................................................285 (cid:2) A Little Polish.................................................................................................................................................287 (cid:2) Core Animation: A Primer....................................................................................................................................288 (cid:2) Core Animation Basics...................................................................................................................................289 (cid:2) Implicit Animations........................................................................................................................................289 (cid:2) Explicit Animations........................................................................................................................................290 (cid:2) Grouping Animations......................................................................................................................................295 (cid:2) What Have We Done?..........................................................................................................................................302 ■Chapter 15: Working with Files...................................................................303(cid:2) (cid:2) Implicit File Access.............................................................................................................................................303 (cid:2) High-level File Operations...................................................................................................................................304 (cid:2) What About That File: The Code.....................................................................................................................305 (cid:2) What About That File: The GUI.......................................................................................................................310 (cid:2) Filing It All Away.................................................................................................................................................314 ■Chapter 16: Concurrency.............................................................................315(cid:2) (cid:2) SlowWorker........................................................................................................................................................316 (cid:2) Threading Basics................................................................................................................................................318 (cid:2) Units of Work.................................................................................................................................................319 (cid:2) Operation Queues..........................................................................................................................................320 (cid:2) Vitalizing SlowWorker.........................................................................................................................................320 (cid:2) Extending NSObject.......................................................................................................................................323 (cid:2) Demanding the Main Thread..........................................................................................................................324 (cid:2) GCD: Low-Level Queuing....................................................................................................................................329 (cid:2) Becoming a Blockhead..................................................................................................................................330 (cid:2) Improving SlowWorker a Second Time..........................................................................................................331 (cid:2) Another Option: NSBlockOperation................................................................................................................333 (cid:2) A Little Concurrency Goes a Long Way...............................................................................................................334 ■Chapter 17: Future Paths.............................................................................335(cid:2) (cid:2) More Cocoa-isms................................................................................................................................................335 (cid:2) Notifications...................................................................................................................................................336 (cid:2) Blocks............................................................................................................................................................337 (cid:2) Cocoa in a Foreign Language.............................................................................................................................339 (cid:2) PyObjC............................................................................................................................................................340 (cid:2) MacRuby........................................................................................................................................................341 (cid:2) Nu..................................................................................................................................................................342 (cid:2) JavaScript......................................................................................................................................................342 (cid:2) F-Script..........................................................................................................................................................343 (cid:2) Ported Cocoa.......................................................................................................................................................343 (cid:2) Cocoa Touch..................................................................................................................................................344 (cid:2) GNUstep and Cocotron...................................................................................................................................344 (cid:2) Cappuccino/Objective-J.................................................................................................................................345 (cid:2) Here at the End of All Things..............................................................................................................................346 ■Index............................................................................................................347 x Foreword Beware Dave Mark bearing new ideas. You’ve probably seen Dave’s name on the cover of the Apress Mac and iPhone books. Actually, his name is on the cover of this book. He’s the Mac and iPhone series editor, he knows how to get stuff done, and I wish I had half of Dave’s energy. And, if you’re a friend of Dave’s, he’ll put you to work. He’s put me to work over the years (in addition to my day job). “Hey, MarkD, I have this idea for an introductory Objective-C book. Why don’t you and Scott Knaster write it?” Sounds like fun, and it was. I still chuckle at some of jokes that we targeted to very narrow slices of nerdom. “Hey, MarkD, Jeff LaMarche and I have this idea for an iPhone SDK book, want to be technical reviewer for it?” Sounds like fun, and it was. Then they did another iPhone book, which I got to review. I discovered that this technical reviewing thing is pretty awesome. Just think of it. You get to read a nerdy book before it comes out. You get to learn cool new stuff along the way. Best of all, you get to poke holes in the text and code, kibitz and generally make random suggestions. And then you don’t actually have to do the work involved in fixing it! And you get paid for it! My spousal overunit lets me keep any money I make off of books, so it’s mine to play with. Daddy’s camera needs a new lens. Anyway, Dave calls up and says, “Hey MarkD, I have this idea for a Cocoa book, want to be the technical reviewer for it?” Sounds like fun. Daddy needs a new 85mm f/1.8. So I “meet” this Jack Nutting guy via email. I GoogleStalk for anything interesting. We chat a bit. I start getting chapters to review. They’re interesting. They’re informative. I’m learning stuff. (oh, so that’s how NSPopupMenu bindings work). And they’ve got a fun streak. I very rarely LOL in real life when I’m at the computer, but there was a time or two where I was totally C|N>K. Then I had to explain “C|N>K” to everyone in the publishing pipeline because I wrote it in the reviewer comments. I had my doubts. Cocoa is a huge topic. The typical Mac OS X desktop application takes a lot more work, and uses a lot more technology and Cocoa classes than the typical iPhone application. Some parts of Cocoa are kind of old and crufty, it being a 20-year-old toolkit. Some parts of Cocoa are totally the new shiny. But no matter what kind of Cocoa app you’re writing, you have to know something about a lot of stuff before you can really get started. Jack’s picked the current best-of-breed techniques for making applications in Mac OS X 10.5 and 10.6 (and beyond). There’s enough breadth to know what you need to know, enough depth to get you up and running, and enough knowledge to dig deeper on your own. I hope you enjoy reading this book. I truly enjoyed reviewing it. Cheers, ++md P.S. C|N>K means “coffee piped through nose into keyboard” Mark Dalrymple xi

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.