Cocoa® Programming Developer's Handbook
Total Page:16
File Type:pdf, Size:1020Kb
Cocoa® Programming Developer’s Handbook This page intentionally left blank Cocoa® Programming Developer’s Handbook David Chisnall Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Cocoa® Programming Developer’s Handbook Editor-in-Chief Copyright © 2010 Pearson Education, Inc. Mark L. Taub All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, Managing Editor or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, John Fuller without written permission from the publisher. No patent liability is assumed with respect to Full-Service the use of the information contained herein. Although every precaution has been taken in Production the preparation of this book, the publisher and author assume no responsibility for errors Manager or omissions. Nor is any liability assumed for damages resulting from the use of the Julie B. Nahil information contained herein. Technical Reviewer ISBN-13: 978-0-321-63963-9 Gregory ISBN-10: 0-321-63963-4 Casamento Book Designer Library of Congress Cataloging-in-Publication Data Gary Adair Chisnall, David. Composition Cocoa programming developer’s handbook / David Chisnall. David Chisnall p. cm. Includes index. ISBN 978-0-321-63963-9 (pbk. : alk. paper) 1. Cocoa (Application development environment) 2. Object-oriented programming (Computer science) 3. Application program interfaces (Computer software) 4. Mac OS. I. Title. QA76.64.C485 2010 005.26’8—dc22 2009042661 Printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan. First Printing December 2009 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Pearson cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Warning and Disclaimer Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The author and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book. Bulk Sales Pearson offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact us by phone or email: U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact the International Sales group: International Sales [email protected] Contents List of Figures xvii List of Tables xxiii Preface xxv I Introducing Cocoa 1 1 Cocoa and Mac OS X 3 1.1 UnderstandingWhentoUseCocoa................ 3 1.1.1 Carbon............................. 3 1.1.2 Java............................... 5 1.1.3 Cocoa.............................. 5 1.1.4 UNIX APIs ........................... 7 1.1.5 OtherChoices......................... 8 1.2 Understanding Cocoa’s Role in Mac OS X . ........... 9 1.2.1 Cocoa.............................. 9 1.2.2 Quartz.............................. 11 1.2.3 CoreFoundation........................ 13 1.2.4 Darwin ............................. 14 1.2.5 XNU............................... 15 1.3 Overview............................... 16 2 Cocoa Language Options 17 2.1 ObjectOrientation......................... 17 2.2 Objective-C............................. 19 2.2.1 Objective-C Compiler Choices . ............. 20 2.2.2 DifferencesfromJavaandC++................ 22 2.2.3 Objective-C2.0......................... 23 2.3 RubyandPython.......................... 24 v vi Contents 2.4 Summary............................... 25 3 Using Apple’s Developer Tools 27 3.1 ObtainingApple’sDeveloperTools................ 28 3.2 InterfaceBuilder........................... 28 3.2.1 Outlets and Actions ...................... 30 3.2.2 CocoaBindings......................... 31 3.2.3 DrawingaSimpleApplication................. 34 3.3 XCode................................ 37 3.3.1 CreatingaSimpleProject................... 37 3.3.2 OpenStep Bundles . ...................... 39 3.3.3 DeveloperExamples...................... 41 3.3.4 Building Without XCode ................... 42 3.4 Objective-C............................. 43 3.4.1 Why Learn Objective-C? ................... 44 3.4.2 AdditionstoC......................... 44 3.4.3 ExceptionsandSynchronization............... 52 3.4.4 Introspection.......................... 55 3.4.5 Objective-CinC........................ 58 3.4.6 Objective-C2.0......................... 60 3.4.7 Blocks.............................. 63 3.4.8 Objective-C++......................... 66 3.5 CocoaConventions......................... 66 3.5.1 Naming . ............................ 66 3.5.2 Memory Management . .................. 70 3.5.3 Constructors and Initializers . ............... 72 3.6 Summary............................... 74 II The Cocoa Frameworks 77 4 Foundation: The Objective-C Standard Library 79 4.1 GeneralConcepts.......................... 79 4.1.1 Mutability . .......................... 80 4.1.2 ClassClusters.......................... 80 4.2 CoreFoundationTypes....................... 85 4.3 Basic Data Types .......................... 85 4.3.1 Non-Object Types . .................... 86 4.3.2 Strings.............................. 87 4.3.3 BoxedNumbersandValues.................. 88 4.3.4 Data............................... 90 4.3.5 CachesandDiscardableData................. 90 Contents vii 4.3.6 DatesandTime........................ 91 4.4 Collections.............................. 93 4.4.1 Comparisons and Ordering . ............... 93 4.4.2 Primitive Collections ...................... 96 4.4.3 Arrays.............................. 97 4.4.4 Dictionaries........................... 98 4.4.5 Sets............................... 99 4.5 Enumeration............................. 100 4.5.1 Enumerating with Higher-Order Messaging . ........ 101 4.5.2 EnumeratingwithBlocks................... 104 4.5.3 SupportingFastEnumeration................. 105 4.6 PropertyLists............................ 111 4.6.1 Serialization........................... 112 4.6.2 User Defaults .......................... 114 4.7 InteractingwiththeFilesystem.................. 117 4.7.1 Bundles............................. 117 4.7.2 Workspace and File Management ............... 120 4.7.3 Working with Paths . .................... 122 4.7.4 FileAccess........................... 123 4.8 Notifications............................. 124 4.8.1 Requesting Notifications . ................... 125 4.8.2 SendingNotifications......................126 4.8.3 Sending Asynchronous Notification . ........... 127 4.8.4 DistributedNotifications....................130 4.9 Summary............................... 132 5 Application Concepts 133 5.1 RunLoops.............................. 133 5.2 ApplicationsandDelegates..................... 140 5.3 TheResponderChain........................ 142 5.3.1 EventDelivery......................... 142 5.3.2 TargetsandActions...................... 146 5.3.3 Becoming First Responder ................... 147 5.4 RunLoopsinApplications..................... 147 5.5 DelegatesandNotifications..................... 151 5.6 TheViewHierarchy......................... 154 5.6.1 Windows............................ 154 5.6.2 Views.............................. 155 5.6.3 Cells............................... 156 5.7 Summary............................... 157 6 Creating Graphical User Interfaces 159 viii Contents 6.1 PositioningViews.......................... 159 6.2 NestedViews............................ 164 6.2.1 ClippedViews......................... 164 6.2.2 ScrollViews........................... 165 6.2.3 TabViews............................ 166 6.2.4 SplitViews........................... 167 6.2.5 Boxes.............................. 169 6.3 CreatingViews........................... 169 6.3.1 Buttons............................. 170 6.3.2 TextViews........................... 172 6.3.3 DataViews........................... 172 6.3.4 Menus.............................. 181 6.4 CocoaBindings........................... 184 6.4.1 Key-ValueCoding....................... 185 6.4.2 Key-ValueObserving......................190 6.4.3 ExposingBindings....................... 193 6.4.4 GenericControllers.......................194 6.4.5 Bindings and User Defaults . ................. 195 6.4.6 Using Bindings in Interface Builder . ............ 196 6.5 Summary............................... 202 7 Windows and Menus 203 7.1 UnderstandingWindows...................... 204 7.1.1 TypesofWindows....................... 207 7.2 Creating Windows .......................... 208 7.3 Creating Window Objects . .................. 208 7.3.1 Displaying Windows ...................... 210 7.3.2 Hiding Windows ........................ 212 7.3.3 UnderstandingWindowControllers..............214 7.3.4 CreatingaSimpleWindowFactory..............214 7.3.5 Saving a Window’s Position .................. 218 7.4 Panels................................ 219 7.4.1 DisplayingStandardPalettes................. 220 7.5 Sheets................................ 223 7.5.1 CreatingSheets......................... 223 7.5.2 ShowingStandardSheets................... 224 7.6 AlertDialogs............................. 229 7.7 Menus................................ 232 7.7.1 The Structure of a Menu . ................ 232 7.7.2