Cocoa Programming by Daniel H Steinberg


37577d6c167edf8.jpg Author Daniel H Steinberg
Isbn 9781934356302
File size 7.27 MB
Year 2010
Pages 454
Language English
File format PDF
Category it ebooks



 

What Readers Are Saying About Cocoa Programming: A Quick-Start Guide for Developers Cocoa Programming is powerful because Daniel Steinberg teaches us the brilliant way Cocoa and Objective C are constructed and commonly used—just what you’d expect from a seasoned, native, local resident. Eric Freeman Author, Head First Design Patterns Over the years, as a programmer experienced in many different languages and paradigms, I’ve come to dread the process of learning new programming languages and technologies. It’s really hard to find a teacher who can speak to experienced programmers without boring us to tears with oversimplification or taking too much prior knowledge for granted. In this book, Daniel Steinberg has proven to be such a teacher. Cocoa Programming exposes the beauty of the Cocoa environment with just enough detail and explanation to help you “get it” the first time. Chad Fowler CTO, InfoEther, Inc. If you are writing applications for the Mac, the iPhone, or the exciting new iPad, this book will get you started. The programming model for all three platforms is essentially the same, and this book will teach it to you. Get this book so you have a solid foundation to write the next big hit. Bill Dudney Gala Factory Software This book is perfect for seasoned developers looking to get started with Cocoa development. Daniel gives you a solid foundation that will allow you to build the next great Mac or iPhone application. James Frye Developer, Tasty Cocoa Software LLC WWW.EBOOK777.COM If you’re new to Mac programming or switching from iPhone development, start reading this book now! Cocoa Programming covers topics other books don’t and puts it all together through great examples where you actually learn it and don’t just read it. Jake Behrens Software Engineer, Snafl As a recently initiated iPhone developer with several applications under my belt (and seasoned web applications developer), this book was a perfect fit for my desire to use my knowledge to create robust, functional, and lightweight Cocoa applications. Daniel Steinberg captured my attention early on with his brilliance and kept me intrigued from one chapter to the next. I had no choice but to write my first Cocoa application while reading and felt I walked away with more than just a solid foundation upon which to build. This book will be a mainstay in my library for sure. Kevin J. Garriott Developer II—Mobile Applications, Rockfish Interactive One of the best flowing programming books I’ve ever read. The chapters just naturally follow one after another. The book is a whole, in much the same way the Cocoa framework is a whole. Both reflect a single, clear, concise voice. Craig Castelaz Principle Software Engineer WWW.EBOOK777.COM WWW.EBOOK777.COM Cocoa Programming A Quick-Start Guide for Developers Daniel H Steinberg The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas WWW.EBOOK777.COM 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 © 2010 Daniel H Steinberg. 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-9343563-0-1 ISBN-13: 978-1-9343563-0-2 Printed on acid-free paper. P1.0 printing, April 2010 Version: 2010-4-22 WWW.EBOOK777.COM Contents 1 2 3 4 Introduction 1.1 Moving In . . . . . . . . . . 1.2 Learning the Language . . 1.3 Installing the Tools . . . . 1.4 Exploring the Frameworks 1.5 In This Book . . . . . . . . Using 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What’s There Creating Your Project in Xcode . . . . . . . . . . Creating the Appearance with Interface Builder Testing the Interface with the Cocoa Simulator . Finishing the Interface . . . . . . . . . . . . . . . Wiring Up the Components . . . . . . . . . . . . Fixing the Build . . . . . . . . . . . . . . . . . . . Sharing Your Browser . . . . . . . . . . . . . . . Exercise: Rinse and Repeat . . . . . . . . . . . . The Nib File . . . . . . . . . . . . . . . . . . . . . Methods and Parameters 3.1 Sending Messages Without Arguments 3.2 Reading the Docs . . . . . . . . . . . . . 3.3 Methods with Arguments . . . . . . . . 3.4 Dynamic Binding . . . . . . . . . . . . . 3.5 Problems Sending Messages . . . . . . . 3.6 Links Back to Yourself . . . . . . . . . . 3.7 Exercise: Multiple Connections . . . . . Classes and Objects 4.1 Creating “Hello, World!” . . . . 4.2 Logging Output to the Console 4.3 Using an Existing Class . . . . 4.4 Refactoring Code . . . . . . . . 4.5 Creating a New Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 15 18 19 20 21 . . . . . . . . . 24 25 26 31 33 35 39 41 42 43 . . . . . . . 48 48 50 53 56 57 58 59 . . . . . 62 62 63 66 69 72 WWW.EBOOK777.COM m CONTENTS www.ebook777.co 4.6 4.7 4.8 4.9 4.10 4.11 4.12 5 Creating and Using a Class Method Creating a New Object . . . . . . . . Further Refactoring . . . . . . . . . . Initializing Your Objects . . . . . . . Logging Objects . . . . . . . . . . . . Exercise: Other Initializations . . . . Solution: Other Initializations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 76 78 79 82 83 84 Instance Variables and Properties 5.1 Pointers . . . . . . . . . . . . . . . . . . . 5.2 Working with Nonobject Types . . . . . 5.3 Getters and Setters . . . . . . . . . . . . 5.4 Converting the Accessors to Properties 5.5 Dot Notation . . . . . . . . . . . . . . . . 5.6 Property Attributes . . . . . . . . . . . . 5.7 Exercise: Adding Properties . . . . . . . 5.8 Solution: Adding Properties . . . . . . . 5.9 Removing Instance Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 87 88 89 92 93 96 98 99 100 6 Memory 102 6.1 Reference Counting . . . . . . . . . . . . . . . . . . . . . 103 6.2 Finding Leaks with the Clang Static Analyzer . . . . . . 104 6.3 Fixing the Memory Leak on Mac OS X . . . . . . . . . . 106 6.4 Properties and Garbage Collection . . . . . . . . . . . . 107 6.5 Creating a Flashlight . . . . . . . . . . . . . . . . . . . . 109 6.6 Finding Leaks in Instruments . . . . . . . . . . . . . . . 111 6.7 Fixing the Memory Leak on the iPhone . . . . . . . . . 112 6.8 Using Zombies . . . . . . . . . . . . . . . . . . . . . . . . 113 6.9 Cleaning Up in dealloc . . . . . . . . . . . . . . . . . . . 115 6.10 Retain and Release in a Setter . . . . . . . . . . . . . . 116 6.11 The Autorelease Pool . . . . . . . . . . . . . . . . . . . . 117 6.12 Using Convenience Constructors . . . . . . . . . . . . . 120 6.13 Exercise: Creating and Using a Convenience Constructor 121 6.14 Solution: Creating and Using a Convenience Constructor 122 7 Outlets and Actions 7.1 The Big Picture . . . . . . . . . . . . . . . 7.2 Using an Outlet . . . . . . . . . . . . . . . 7.3 Exercise: Creating and Using an Outlet . 7.4 Solution: Creating and Using an Outlet . 7.5 Declaring an Action . . . . . . . . . . . . . 7.6 Connecting and Implementing the Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WWW.EBOOK777.COM 124 125 125 128 129 130 133 Report erratum this copy is (P1.0 printing, April 2010) 8 m CONTENTS www.ebook777.co 7.7 7.8 7.9 7.10 7.11 7.12 8 9 Exercise: Hiding the Button . . Solution: Hiding the Button . . Exercise: Toggling the Interface Solution: Toggling the Interface Introducing Another Outlet . . Creating Selectors from Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 135 136 136 137 139 Creating a Controller 8.1 How We’ve Created Objects . . . . . . . . . . . . 8.2 Creating Our Controller Class . . . . . . . . . . . 8.3 Creating an Instance of Our Controller in IB . . 8.4 Declaring an Outlet and an Action . . . . . . . . 8.5 Forward Declaration . . . . . . . . . . . . . . . . 8.6 Wiring Up the Controller . . . . . . . . . . . . . . 8.7 Implementing the Loading of the Previous Page 8.8 Exercise: Finishing the Controller . . . . . . . . 8.9 Solution: Finishing the Controller . . . . . . . . 8.10 Awake from Nib . . . . . . . . . . . . . . . . . . . 8.11 Disabling and Enabling the Buttons . . . . . . . 8.12 Still Needs Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 141 143 144 146 149 150 150 150 151 152 153 156 Customizing with Delegates 9.1 Understanding Delegates . . . . . . . . . . . . . . 9.2 The Default Window Behavior . . . . . . . . . . . . 9.3 Turning the Background Red . . . . . . . . . . . . 9.4 Exercise: Turning the Background Green . . . . . 9.5 Solution: Turning the Background Green . . . . . 9.6 Application Delegate . . . . . . . . . . . . . . . . . 9.7 Delegates for Your Web View . . . . . . . . . . . . . 9.8 Setting the Window Title . . . . . . . . . . . . . . . 9.9 Exercise: Updating the URL and Setting Buttons 9.10 Solution: Updating the URL and Setting Buttons . 9.11 Cleaning Up . . . . . . . . . . . . . . . . . . . . . . 9.12 Exercise: Adding a Progress Indicator . . . . . . . 9.13 Solution: Adding a Progress Indicator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 158 162 162 165 165 165 166 167 169 170 170 173 173 . . . . . 175 175 177 178 179 181 10 Adapting Our Browser to the iPhone 10.1 Creating the iPhone Project . . . . 10.2 Creating the Look of Our Browser 10.3 The WebView’s Limitations . . . . . 10.4 Loading a Web Page at Launch . . 10.5 Tweaking the Text Field in IB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 9 m CONTENTS www.ebook777.co 10.6 10.7 10.8 10.9 10.10 Using the Text Field Delegate . . . . . . . . . . . . Using a Third Delegate to Implement the Buttons Exercise: Adding an Activity Indicator . . . . . . . Solution: Adding an Activity Indicator . . . . . . . Organizing with Pragma Marks . . . . . . . . . . . 11 Posting and Listening for Notifications 11.1 Exercise: Creating a Model . . . . . . . . . . 11.2 Solution: Creating a Model . . . . . . . . . . 11.3 Registering for Notifications . . . . . . . . . . 11.4 Responding to Workspace Activity . . . . . . 11.5 Holding on to the Controller . . . . . . . . . . 11.6 Exercise: Registering for Notifications . . . . 11.7 Solution: Registering for Notifications . . . . 11.8 Posting Notifications . . . . . . . . . . . . . . 11.9 Exercise: Receiving the Custom Notifications 11.10 Solution: Receiving the Custom Notifications 12 Creating Protocols for Delegation 12.1 Exercise: Creating and Setting the Delegate . 12.2 Solution: Creating and Setting the Delegate . 12.3 Creating and Using a Protocol . . . . . . . . . 12.4 Requiring Methods . . . . . . . . . . . . . . . 12.5 Responding to Selector . . . . . . . . . . . . . 12.6 Exercise: Calling the Delegate Methods . . . 12.7 Solution: Calling the Delegate Methods . . . 12.8 Exercise: Cleaning Up . . . . . . . . . . . . . 12.9 Solution: Cleaning Up . . . . . . . . . . . . . 13 Working with Dictionaries 13.1 Looking at the User Info . . . . . . . 13.2 Reading from a Dictionary . . . . . . 13.3 Exercise: Displaying the Name . . . 13.4 Solution: Displaying the Name . . . 13.5 Reducing Redundancy . . . . . . . . 13.6 Using a Dictionary for Flow Control 13.7 Adding and Removing Entries with a 13.8 Exercise: Adding an Icon . . . . . . . 13.9 Solution: Adding an Icon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 184 185 186 188 . . . . . . . . . . 191 192 192 194 195 197 198 198 199 201 201 . . . . . . . . . 203 204 204 205 207 208 208 209 209 209 213 . . . . . . . . . . . 213 . . . . . . . . . . . 214 . . . . . . . . . . . 215 . . . . . . . . . . . 215 . . . . . . . . . . . 216 . . . . . . . . . . . 218 Mutable Dictionary 218 . . . . . . . . . . . 221 . . . . . . . . . . . 223 WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 10 m CONTENTS www.ebook777.co 14 Multiple Nibs 14.1 Methods, Objects, and Nibs . . . . 14.2 Splitting Nibs . . . . . . . . . . . . 14.3 Preparing to Split Out the View . . 14.4 Creating the View Nib . . . . . . . 14.5 Integrating a Nib File . . . . . . . . 14.6 The File’s Owner . . . . . . . . . . 14.7 Exercise: Loading the View . . . . 14.8 Solution: Loading the View . . . . 14.9 Creating the Window Nib . . . . . . 14.10 Loading the Window Nib . . . . . . 14.11 Presenting the Window . . . . . . . 14.12 Exercise: Connecting the View and 14.13 Solution: Connecting the View and . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . the Model the Model . 15 Creating Custom Views 15.1 Creating a Custom View . . . . . . . 15.2 Drawing Shapes into a Custom View 15.3 Exercise: Changing the Stroke Color 15.4 Solution: Changing the Stroke Color 15.5 Drawing Images . . . . . . . . . . . . 15.6 Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 227 229 231 232 233 235 236 237 237 239 240 240 241 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 243 245 248 248 250 252 16 Displaying Data in a Table 16.1 Tables and Data Sources . . . . . . . . . . . 16.2 Exercise: Implementing a Basic Data Source 16.3 Solution: Implementing a Basic Data Source 16.4 Exercise: Introducing a Data Source . . . . . 16.5 Solution: Introducing a Data Source . . . . . 16.6 Filling Cells Based on Table Column Titles . 16.7 Table Column Identifiers as Keys . . . . . . . 16.8 Previews of Coming Attractions . . . . . . . . 16.9 Exercise: Adding and Removing Rows . . . . 16.10 Solution: Adding and Removing Rows . . . . 16.11 Manually Removing Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 256 259 260 261 262 263 265 266 266 267 268 . . . . 270 270 273 274 275 17 Saving Data to Disk 17.1 Saving in Your Running Application 17.2 Where to Put Application Support . 17.3 Saving to a Plist . . . . . . . . . . . . 17.4 Reading a Plist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 11 m CONTENTS www.ebook777.co 17.5 17.6 17.7 17.8 17.9 17.10 Saving an Archive to Disk . . . . . Reading and Using Preferences . . Setting the Factory Defaults . . . . Preparing to Set User Defaults . . The Preference Window Nib . . . . Enabling the Preferences Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 278 278 280 281 283 18 Changing Views 18.1 Working with Radio Buttons . . . . . . . . 18.2 Adding Preferences for View at Launch . 18.3 Exercise: Launching with the Right View 18.4 Solution: Launching with the Right View 18.5 Eliminating Magic Numbers . . . . . . . . 18.6 Customizing the Menu Bar . . . . . . . . 18.7 Moving the Main Window . . . . . . . . . 18.8 Exercise: Switching Views (Mostly) . . . . 18.9 Solution: Switching Views (Mostly) . . . . 18.10 Lazy Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 285 287 288 288 290 292 293 294 294 296 19 Key Value Coding 19.1 Treating Objects Like Dictionaries . . . . . 19.2 Getting Variables Using KVC . . . . . . . . 19.3 Undefined Keys . . . . . . . . . . . . . . . . 19.4 Exercise: Setting Variables Using KVC . . . 19.5 Solution: Setting Variables Using KVC . . . 19.6 KVC and Dictionaries . . . . . . . . . . . . . 19.7 Keypaths for Navigating a Class Hierarchy 19.8 Exercise: Filling Tables Using KVC . . . . . 19.9 Solution: Filling Tables Using KVC . . . . . 19.10 Arrays and KVC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298 299 301 303 304 304 305 306 309 309 311 . . . . . . . . . 314 314 316 318 320 321 323 324 324 325 20 Key Value Observing 20.1 Codeless Connections . . . . . . . . 20.2 A Target-Action Counter . . . . . . . 20.3 Introducing an Observer . . . . . . . 20.4 Registering an Observer . . . . . . . 20.5 Making Changes Observable . . . . 20.6 Observing the Changes . . . . . . . . 20.7 Exercise: Adding a Second Observer 20.8 Solution: Adding a Second Observer 20.9 The Ugly Way to Observe More Than . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . One Attribute . . . . . . . . . WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 12 m CONTENTS www.ebook777.co 20.10 Selecting Methods Using KVC . . . . . . . . . . . . . . . 20.11 Implementing an Observer Object . . . . . . . . . . . . 20.12 Updating Dependent Variables . . . . . . . . . . . . . . 328 329 331 21 Cocoa Bindings 21.1 The Model and View for Our Counter with Bindings 21.2 Creating and Connecting the NSObjectController . 21.3 Binding More Objects . . . . . . . . . . . . . . . . . . 21.4 Number Formatters . . . . . . . . . . . . . . . . . . . 21.5 Exercise: Connecting Two Counters with Bindings . 21.6 Solution: Connecting Two Counters with Bindings . 21.7 The Model for Our Bookshelf Example . . . . . . . . 21.8 Creating the View for the Bookshelf . . . . . . . . . 21.9 Binding with the NSArrayController . . . . . . . . . 21.10 The Big Finish . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 335 335 338 339 341 342 344 345 346 349 22 Core Data 22.1 Entities and Attributes . . . . . . . . . . 22.2 Using the Core Data Widget . . . . . . . 22.3 The Managed Object Context . . . . . . 22.4 The Persistence Layer . . . . . . . . . . 22.5 Introducing Relationships . . . . . . . . 22.6 Choosing a Relationship’s Delete Rule . 22.7 Updating the View . . . . . . . . . . . . 22.8 Managing Dependencies . . . . . . . . . 22.9 Exercise: Enabling Author Addition and 22.10 Sorting . . . . . . . . . . . . . . . . . . . 22.11 Filtering Items . . . . . . . . . . . . . . . 22.12 Coding the Sort Descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 351 352 354 356 358 360 361 362 363 363 365 366 23 Categories 23.1 Overcoming Limitations . . . . . . . . . . . . . . . . . 23.2 Creating a Category . . . . . . . . . . . . . . . . . . . . 23.3 Category Cautions . . . . . . . . . . . . . . . . . . . . 23.4 Private Methods in Class Extensions . . . . . . . . . . 23.5 Exercise: Extending Properties with Class Extensions 23.6 Solution: Extending Properties with Class Extensions 23.7 Categories and Core Data . . . . . . . . . . . . . . . . 23.8 Generated Classes in Core Data . . . . . . . . . . . . 23.9 Accessing Properties . . . . . . . . . . . . . . . . . . . 23.10 Regenerating Class Files from Entities . . . . . . . . . . . . . . . . . . . 368 368 369 371 372 375 375 376 378 379 380 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Removal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 13 m CONTENTS www.ebook777.co 24 Blocks 24.1 The Need for Blocks in Wrappers . . . 24.2 Declaring a Block . . . . . . . . . . . . 24.3 Using Blocks in Wrappers . . . . . . . 24.4 Capturing Values . . . . . . . . . . . . 24.5 Blocks and Collections . . . . . . . . . 24.6 Declaring, Defining, and Using Blocks 24.7 Using __block . . . . . . . . . . . . . . 24.8 Cleaning Up with typedef . . . . . . . 24.9 Exercise: Using Blocks in Callbacks . 24.10 Solution: Using Blocks in Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 384 385 386 387 389 390 392 393 394 396 25 Operations and Their Queues 25.1 Making the Beach Ball Spin . . . . . . . . . . 25.2 Invocation Operations . . . . . . . . . . . . . 25.3 Block Operations . . . . . . . . . . . . . . . . 25.4 Interacting with the Queue and Operations . 25.5 Custom NSOperations . . . . . . . . . . . . . 25.6 From Operation Queues to Dispatch Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 398 400 402 403 406 408 . . . . . . . . . 412 412 414 415 416 418 419 420 421 422 . . . . 426 426 427 428 429 26 Dispatch Queues 26.1 When to Use Dispatch Queues . . 26.2 Quick Queue Overview . . . . . . . 26.3 Drawing Our Fractal . . . . . . . . 26.4 Working Without Dispatch Queues 26.5 The Main Queue . . . . . . . . . . . 26.6 Global Concurrent Queues . . . . 26.7 Synchronizing on the Main Queue 26.8 Private Dispatch Queues . . . . . . 26.9 Synchronous Tasks . . . . . . . . . 27 Up and Out 27.1 But What About... . 27.2 What’s Next . . . . 27.3 Acknowledgments . 27.4 Dedication . . . . . A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bibliography 431 Index 434 WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 14 Chapter 1 Introduction As I finished up the final walk-through of our new house, a woman called to me from across the street. “Tonight’s our annual progressive dinner,” she shouted. “Come meet the neighborhood.” I followed along and met our new neighbors all at once. It went fast and was a bit overwhelming, and there was a ton of information, some of which I was able to sort out later. Mostly, it made me feel a lot better about my new neighborhood. I knew the questions to ask, and I had met the people who could answer them for me. That’s the goal in this book. It’s not a guide for tourists that lists the things you’d want to see if you were only going to live with Cocoa for a day. It’s not a comprehensive almanac that lists every API class by class and method by method. This is designed to get you through those first weeks and months of moving to Cocoa. This is the coding equivalent of finding out where to go for coffee, which streets are safe to walk on at night, and which teacher to request for your kids. Once you get a feel for the neighborhood, you’ll have more questions, but you’ll know where and how to get them answered. 1.1 Moving In Moving to Cocoa is like moving to a new neighborhood. You need to figure out where everything is and get used to the local customs. You’ll find that some aspects of developing Cocoa apps for Mac OS X are very similar to what you’ve been doing, while other aspects feel very strange. In this book you’ll get a feel for working with the following: • Objective-C: The language of Cocoa development WWW.EBOOK777.COM www.ebook777.coM m OVING I N • Xcode, Interface Builder, and Instruments: The tools for Cocoa development • Cocoa: The frameworks full of existing classes created by Apple that will give your applications the features and polish of your favorite Mac OS X applications What, you ask, is a framework? A framework is a directory that contains some related set of resources. You can think of a framework as a library or a package, but it can also contain image files, documentation, localization strings, and other constructs that you’ll learn about later in this book. You’ll see in Chapter 2, Using What’s There, on page 24 that we bring in all the resources for programming web applications by adding the WebKit framework to our project. Frameworks are kind of like what you know already and kind of different. For now you can think of them as libraries, and you’ll be fine. You’ll learn about many ideas in Mac OS X development that are close to what you already know. You’ll be tempted to hold on the way you used to do things. Don’t. You don’t want to be the only one driving on the wrong side of the road. It’s not good for you, and it’s not good for others sharing the same road. No one likes a new neighbor who goes on about how good it was where they came from. It’s the same here in OS X. It isn’t that the old-timers are being mean. It’s just that they have a way of doing things. You will have an incredible amount of power at your fingertips to quickly develop native Mac OS X applications if you embrace Objective-C, use the development tools, and take advantage of the Cocoa frameworks. You will tend to get much further much more quickly if you use what is provided for you and follow local customs rather than fight with the culture. Use Objective-C. Sure, you can write Cocoa applications in other languages. But for now, learn the native language. There is a lot of support for new developers on the various Apple lists1 and in the support documentation, tutorials, and sample code accessible from Xcode. You will have an easier time of getting your question answered if you use the lingua franca of Cocoa development. For a comprehensive list, visit http://lists.apple.com/. You will probably want to subscribe to the http://lists.apple.com/mailman/listinfo/cocoa-dev list. Also look for lists that serve specific areas that you target in your application. If you have a specific need that is only temporary, you can also search the archives. 1. WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 16 www.ebook777.coM m OVING I N What About the iPhone and iPad? In this book we mainly target Mac OS X development. For the most part, these are the same techniques, tools, and APIs you will use to target the iPhone and iPad. There are differences that I’ve highlighted in a couple of the iPhone chapters, but for the most part this book focuses on desktop Cocoa and assumes that you’ll find it fairly easy to move from Cocoa development for Mac OS X to developing for the mobile platforms. Use the tools that Apple provides for Cocoa development. In your old environment, you may have popped open a terminal window and used vi or emacs along with gcc and gdb, or you may have used an IDE like Eclipse. For Cocoa app development, use Xcode to write, compile, debug, and run your code. Use Xcode to create, build, and manage your projects. You’ll even use Xcode to create and edit your data models. You’ll use Interface Builder (IB) to create your GUI and to wire up the various components. You’ll use Instruments to improve the performance of your application. You can find most of your favorite commandline developer tools in /usr/bin/, but you still want to use Apple’s dev tools when you are creating a Cocoa app. Finally, use the built-in frameworks as much as you possibly can. Before you think about writing any code, take a look at what Apple has provided for you. Your first impulse should always be to use what is there. To emphasize this last point, your first project will be to build a simple web browser with Apple’s WebKit framework.2 The browser will include a text field where the user can enter a URL and a web view that renders the web page. You will also add Forward and Back buttons for navigating through sites you have already visited. Because you are taking advantage of the WebKit framework, you can accomplish all of this without writing any code. Working with the new language, tools, and APIs is going to feel a bit odd at first. They are unfamiliar, so your first instincts won’t always be right. but in no time you’ll be typing in what you assume the method I got the idea for starting with this example while editing Chris Adamson’s “ten-minute browser” example in iPhone SDK Development [DA09]. 2. WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 17 www.ebooLk777.coL m EARNING THE ANGUAGE Joe Asks. . . Is This Book for Me? This book is for the experienced programmer who is new to the Mac or iPhone platform. You understand basic programming concepts but just don’t know how they apply in this setting. You know a language that has a structure somewhat like C, but you don’t know ObjectiveC. You understand object-oriented programming, just not in this setting. You want to learn all the techniques for working with the Cocoa frameworks, but you are comfortable reading the documentation to explore the specific APIs that you need in your app. Finally, you have a Mac and are currently running Snow Leopard. By the way, if you are a new programmer, start with our book by Tim Isted, Beginning Mac Programming, which is available at http://pragprog.com/titles/tibmac/. name is and find that it is, in fact, correct. Once you tune yourself to the Cocoa frameworks, you’ll find that they tend to obey the principle of least surprise: you’ll usually find what you expect to find. This is not a comprehensive book in any way. This book doesn’t cover every nook and cranny of Objective-C. I don’t take you click by click through all that you can do in Xcode nor do we walk through the entire set of APIs. This book gets you up and running and gives you enough context to find the answers you need as new questions arise. 1.2 Learning the Language When you first learned to speak a new language in school, you probably translated everything back and forth from and to your native language. After a lot of work, you began to master the vocabulary and the grammar and you became comfortable with the native usage patterns and idioms. Without noticing it, one day you found yourself thinking in your new language while you were speaking or reading it. The same is true about Objective-C, the language of Cocoa. The syntax is different, but much of it is similar to languages you use now. You WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 18 7.comT I www.ebook77 NSTALLING THE OOLS need to be as careful of being fooled by the similarities as you are of being challenged by the differences.3 You can get used to the square brackets and the way that code is structured pretty quickly. You also have to get comfortable with the common patterns that Cocoa programmers use. In Chapter 3, Methods and Parameters, on page 48, we’ll get you comfortable reading some Objective-C. We’ll start with messages because sending messages is the core of Cocoa programming. Even experienced object-oriented programmers lose sight of this. We tend to think that OO is all about the objects. Objective-C sits on top of C, but it owes many of its ideas to Smalltalk. It helps to reread Alan Kay’s 1998 reminder on the Squeak mailing list every now and then in which he expresses his regret at coining the term objects because it encourages people to focus on the wrong thing.4 Kay explains that “The key in making great and growable systems is much more to design how its modules communicate rather than what their internal properties and behaviors should be.” 1.3 Installing the Tools Check that you have installed the free developer tools. By default the installer puts the developer applications, documentation, examples, and other files in the Developer directory at the root level. Even though the developer tools are free and included on the install discs that come with Mac OS X, they are not installed by default. Check the Developer directory to make sure they are installed. Also select Xcode and choose File > Get Info or press D I to bring up the info window for Xcode. Check the version number. The examples in this book assume you are running at least Xcode 3.2. Get the most recent developer tools (including beta releases) by joining the Apple Developer Connection (ADC) at http://developer.apple.com/. You should join the ADC. There is currently a free membership level that gives you access to much of the prerelease software. There are also paid membership levels that come with different benefits. One of the great selling points for Java was also its weakness. Its syntax was familiar. C programmers could easily write Java code. But they often wrote Java code that looked a little too much like C code. The same is true for many other programming languages including Objective-C. Objective-C sits on top of C, so you could write pure C code. Don’t. 3. 4. http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 19 m k777.co E F www.eboo XPLORING THE RAMEWORKS Starting Fresh You might have problems if you are moving to Snow Leopard from an earlier version of Mac OS X or if you are moving to the developer tools installation that features Xcode 3.2 from an earlier version of the developer tools. These problems manifest themselves in different ways but are often surface when dealing with nib files. The fix is simple. First uninstall your old developer tools, and then do a clean install of the new developer tools. Problem solved. We’ll spend most of our time in this book in Xcode and Interface Builder. You’ll write your code in Xcode and design the data models that we’ll use later in the book. You’ll use Interface Builder to create the look of your application and to connect your visual components to the code containing your business logic. Even though those two applications get most of the attention, you get a lot of other tools for free. For example, before you release an application into production, you’re going to want to take some time to exercise it with profiling tools such as Instruments and Shark. You’ll also find audio tools, graphic tools, other performance tools, and a slew of utilities. When you install the developer tools, you’ve also installed a ton of command-line tools as well. 1.4 Exploring the Frameworks We’re going to play quite a bit with the Cocoa frameworks. When you can, you should use the objects and classes that Apple provides before you struggle to write the code yourself. In the beginning, you will find yourself writing a lot of code to do something you’ve seen other applications do on Mac OS X. An experienced Cocoa developer will look at your code, make a face, and suggest, “Why don’t you just...?” As much as you may hate to hear it, their two lines of code will do everything that your 400 lines did. That’s just the way it’s going to be. And then one day it will all make sense to you. That doesn’t mean you will know the two lines of code you need to write. But fifty lines in, you’ll be aware that you’re working too hard. You will WWW.EBOOK777.COM Report erratum this copy is (P1.0 printing, April 2010) 20

Author Daniel H Steinberg Isbn 9781934356302 File size 7.27 MB Year 2010 Pages 454 Language English File format PDF Category IT ebooks Book Description: FacebookTwitterGoogle+TumblrDiggMySpaceShare Apple’s Cocoa frameworks let you write powerful and attractive applications for Mac OS X or the iPhone. With this book plus your existing knowledge of object-oriented programming you can take advantage of Cocoa and create compelling, feature rich, compliant Mac applications for this industry-leading environment using XCode 3.     Download (7.27 MB) Cocoa Programming Developer’s Handbook Core Data Beginning Mac OS X Snow Leopard Programming Beginning Mac Programming Cocoa and Objective-C: Up and Running Load more posts

Leave a Reply

Your email address will not be published. Required fields are marked *