ptg6935296 Joshua Nozzi Mastering Xcode 4 DEVELOP and DESIGN Spine width: 0.664” ptg6935296 Joshua Nozzi Mastering Xcode 4 DEVELOP and DESIGN ptg6935296 Mastering Xcode 4: Develop and Design Joshua Nozzi Peachpit Press 1249 Eighth Street Berkeley, CA 94710 510/524-2178 510/524-2221 (fax) Find us on the Web at: www.peachpit.com To re p or t er ro rs , p le as e sen d a n ote to e rr at a@p ea ch pi t. co m Peachpit Press is a division of Pearson Education Copyright © 2012 by Joshua Nozzi Editor: Cliff Colby Production editor: Myrna Vladic Development editor: Kim Wimpsett and Robyn G. Thomas Copyeditor: Scout Festa Te ch n ic a l E di t or : Duncan Campbell Cover design: Aren Howell Straiger Interior design: Mimi Heft Compositor: David Van Ness Indexer: Ann Rogers Notice of Rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, elec- tronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the pub- lisher. For information on getting permission for reprints and excerpts, contact permissions@peachpit.com. Notice of Liability The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press 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 instructions contained in this book or by the computer software and hardware products described in it. Trademarks Xcode is a trademark of Apple Inc., registered in the United States and other countries. Many of the designa- tions used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit Press was aware of a trademark claim, the designa- tions appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, isintended to convey endorsement or other affiliation with this book. ISBN 13: 978-0-321-76752-3 ISBN 10: 0-321-76752-7 9 8 7 6 5 4 3 2 1 Printed and bound in the United States of America ptg6935296 Thanks to all my peers, friends, and family for their enthusiastic support, to a great team of professionals for helping me reach this goal, and to Matt for putting up with yet another of my time-consuming projects. ptg6935296 IV MASTERING XCODE ACKNOWLEDGMENTS I wish to thank the following people whose work I used while writing this book. CYRIL GODEFROY Cyril’s masterfully broken code examples demonstrated some nice highlights of the Clang Static Analyzer. You can find them at http://xcodebook.com/cgodefroy. COLIN WHEELER Colin’s Xcode shortcut cheat sheet saved me loads of tedium when creating AppendixB. You can find the original, downloadable version that Colin maintains at http://xcodebook.com/cwheeler. ptg6935296 Contents V Introduction x Wel co me to Xcod e xii PART I THE BASICS: GETTING STARTED WITH XCODE 1 DISCOVERING XCODE TOOLS Downloading 4 Installing 4 Exploring 5 Wrapping Up 7 2 STARTING A PROJECT 8 Wel co me to Xcod e! 10 Creating a New Project 11 Project Modernization 15 Building and Running an Application 17 Wrapping Up 17 3 NAVIGATING A PROJECT 18 The Workspace Window 20 The Navigator Area 21 The Jump Bar 26 The Editor Area 27 The Utility Area 31 The Debug Area 32 The Activity Viewer 33 The Tabs 34 The Organizer Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Wrapping Up 35 4 GETTING HELP The Help Menu 38 The Organizer’s Documentation Tab 39 The Source Editor 41 Community Help and Feedback 42 Wrapping Up 43 CONTENTS ptg6935296 VI MASTERING XCODE PART II WORKING WITH COCOA APPLICATIONS 5 CREATING USER INTERFACES Understanding Nibs 48 Getting Familiar with Interface Builder 50 Adding User Interface Elements 55 Layout 58 Wrapping Up 69 6 ADDING FILES TO A PROJECT Adding Existing Files 72 Creating New Files 74 Using the File Template Library 76 Removing Files from the Project 77 Wrapping Up 77 7 WRITING CODE WITH THE SOURCE EDITOR Exploring the Source Editor Interface 80 Navigating Source Code 81 Using Code Completion 84 Exploring the Code Snippet Library 85 The Assistant 87 Wrapping Up 87 8 SEARCHING AND REPLACING Using the Search Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Searching within Files 97 Wrapping Up 97 9 BASIC DEBUGGING AND ANALYSIS Compile-Time Debugging 100 Runtime Debugging 102 Wrapping Up 109 10 USING THE DATA MODEL EDITOR 110 Introducing Core Data 112 Using the Data Model Editor 115 ptg6935296 Contents VII Creating a Basic Data Model 117 Creating a UI for the Model 118 Using the Assistant 125 Wrapping Up 125 11 CUSTOMIZING THE APPLICATION ICON Picking the Ideal Artwork 128 Creating Icons 129 Setting the Application Icon 131 Setting Document Icons 133 Wrapping Up 133 12 DEPLOYING AN APPLICATION Archiving 136 Alternatives to Archiving 140 Wrapping Up 141 PART III GOING BEYOND THE BASICS 13 ADVANCED EDITING Renaming Symbols 146 Refactoring 147 Organizing with Macros 150 Changing Editor Key Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Jump to Definition 152 My Company Name 153 Wrapping Up 153 14 THE BUILD SYSTEM An Overview 156 Wor kin g w it h Targe ts 159 Wor kin g w it h Sch emes 178 Entitlements (Sandboxing) 191 Wrapping Up 193 ptg6935296 VIII MASTERING XCODE 15 LIBRARIES, FRAMEWORKS, AND LOADABLE BUNDLES What are Libraries, Frameworks, and Bundles? 196 Using Existing Libraries and Frameworks 199 Creating a Framework 208 Wrapping Up 215 16 WORKSPACES What Is a Workspace? 218 When to Use a Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Creating a Workspace 221 Another Kind of Workspace 228 Wrapping Up 231 17 DEBUGGING AND ANALYSIS IN DEPTH Using the Clang Static Analyzer 234 Exploring Analyzer Results 236 Threads and Stacks 242 Inspecting Memory 246 Conferring with the Console 250 Viewing Generated Output 258 Debugging Apps for iOS Devices 260 Wrapping Up 269 18 UNIT TESTING 270 What is Unit Testing? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Unit Testing in Xcode 276 Writing a Unit Test 284 Adding Unit Tests to Existing Projects 295 Wrapping Up 297 19 USING SCRIPTING AND PREPROCESSING Extending Your Workflow with Custom Scripts 300 Using the Preprocessor 313 Wrapping Up 322 ptg6935296 Contents IX chapter 20 USING INSTRUMENTS An Overview of DTrace 326 A Tour of Instruments 327 Using Instruments for Common Tasks 339 Wrapping Up 348 chapter 21 SOURCE CODE MANAGEMENT Xcode Snapshots 352 Using an SCM System 356 Wrapping Up 373 Index 374 APPENDIXES appendix a MANAGING YOUR iOS DEVICES A appendix B GESTURES AND KEYBOARD SHORTCUTS B appendix c DOCUMENTATION UPDATES C appendix d OTHER RESOURCES D [...]... Branch, merge, pull, push, and resolve conflicts all from within Xcode Xiii This page intentionally left blank Part I The Basics: gettIng stArted wiTh Xcode 4 1 1 Discovering Xcode tools Xcode 4 is the flagship application of Xcode Tools, Apple’s suite of developer tools It is aimed squarely at developing, testing, and packaging Mac OS and iOS applications, utilities, and plug-ins written with the Cocoa... build commands and liked it Uphill Both ways In the snow Then again, why let those newbies outpace you? Xcode puts the same powerful tools you know (and some new ones you may not) in your hands Despite its shiny, easy-to-use interface, a lot of power lurks just under the INTERfACE BuIlDER ClANG STATIC ANAlyzER Build and edit rich user interfaces with Interface Builder Drag and drop outlets and actions... problem is broken down step by step surface Xcode 4 lets you write and manage your code, design and build user interfaces, analyze and debug your apps, and more So what if it takes you less time? INSTRuMENTS SOuRCE CODE MANAGEMENT Trace and profile your code with Instruments Follow your application’s activity through time to find and analyze performance problems and more Manage your source code with the... MASTERING XCODE 4 WhAT yOu WIll lEARN This book is divided into three major parts and includes four appendixes on the book’s companion Web site Part I: The Basics: Getting Started with Xcode 4 In very short order, you’ll install Xcode and get down to business building a useless application Nobody but perhaps your mother would buy it, but it very neatly demonstrates the Xcode 4 project workflow and how... design your UI, build your data models, write and run unit tests, and build and package your apps and plug-ins You’ll spend most of your development time in Xcode Some of these tools can be launched automatically from within Xcode (Instruments and the iOS Simulator, for example) This will be covered in later chapters Instruments Instruments is Apple’s profiling and analysis tool We’ll briefly visit Instruments... Row and Cover Flow, and more The compositions can be self-contained or accept input from your application to affect various properties Even non-developers can enjoy Quartz Composer, because it does not require writing a single line of code 6 ChAPTER 1 dIsCoverIng XCode tools WRAPPING up Plenty of other helpful utilities (both GUI and command-line) exist in addition to those covered here Consult the Xcode. .. applications, there are templates for command-line tools, AppleScript applications, frameworks, bundles, plug-ins, Spotlight plug-ins, IOKit drivers, and more In this chapter, you’ll create a basic Cocoa application, which you’ll use throughout this book to explore Xcode 9 welcome TO Xcode! fiGURe 2 1 The Welcome to Xcode window You can find Xcode in the /Developer/Applications folder (assuming you... is an intermediate-level introduction to Xcode 4, Apple’s integrated development environment It assumes you have some development experience and are familiar with the Cocoa API It won’t teach you how to write code or much at all about Cocoa There are other books for that This one is strictly focused on how to use Xcode itself, whatever your development endeavors Of course, since Xcode is most often... given that Xcode seems to be peacefully awaiting orders you can turn off this automatic updating in the Documentation panel of Xcode s preferences 4 ChAPTER 1 dIsCoverIng XCode tools eXplorIng Now that you’ve installed Xcode Tools, you can find it on your system disk under the /Developer folder unless you’ve chosen to install elsewhere You’ll find Xcode and a number of other applications in the /Developer/Applications... on the Xcode 4 application, there are other important tools with which you should become familiar Let’s explore some of the tools included in the suite ThE BIG TOOlS There are three important applications in which you’ll spend most of your time XCode The Xcode integrated development environment (IDE) is the star of the suite With Xcode, you create and manage projects, write and debug your code, design . ptg6935296 Joshua Nozzi Mastering Xcode 4 DEVELOP and DESIGN Spine width: 0.6 64 ptg6935296 Joshua Nozzi Mastering Xcode 4 DEVELOP and DESIGN ptg6935296 Mastering. DESIGN ptg6935296 Mastering Xcode 4: Develop and Design Joshua Nozzi Peachpit Press 1 249 Eighth Street Berkeley, CA 947 10 510/5 2 4- 2178 510/5 2 4- 2221 (fax) Find us