Mastering Xcode 4: Develop and Design
Joshua Nozzi
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 report errors, please send a note to
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
Technical Editor: 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, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact .
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 designations 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 designations 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, is intended 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
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.
Acknowledgments
I wish to thank the following people whose work I used while writing this book.
Cyril Godefroy
Cyrils masterfully broken code examples demonstrated some nice highlights of the Clang Static Analyzer. You can find them at http://xcodebook.com/cgodefroy.
Colin Wheeler
Colins Xcode shortcut cheat sheet saved me loads of tedium when creating .
Introduction
This book is an intermediate-level introduction to Xcode 4, Apples integrated development environment. It assumes you have some development experience and are familiar with the Cocoa API. It wont 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 used with the Cocoa API and Objective-C, there are basic introductions to Cocoa concepts and a few trivial code samples sprinkled here and there to illustrate various points. In these cases, I point to the documentation that Apple provides (to save you some trouble looking it up), but I only had a limited number of pages in which to show you Xcode stuff, so please keep this in mind when writing your scathing Amazon reviews.
Also, Ive formed the opinion that Apple is crafty when it comes to software releases. Not only are they ultra-secretive, but they appear to know my precise schedule and plans (I blame iCloud). They seem to use this knowledge to wait until Im almost finished and then change a bunch of stuff in a single release, necessitating the tracking down and editing of many fine details. I imagine an Apple overseer watching me through my Macs camera, stroking a wrinkly, hairless cat and waiting until Im almost finished. He then orders his henchmen to release the next set of random changes and leans toward the screen expectantly, muttering Yeeesssss... as I shake my fist at the sky and shout his name in dramatic fashion. The cat, of course, is hairless to avoid messing up his black turtleneck.
Whatever the case, I may say things that no longer apply to some future version or mention menus that no longer exist as such. Sorry. Blame Apple. Then buy my next edition.
What You Will Learn
This book is divided into three major parts and includes four appendixes on the books companion Web site.
Part I: The Basics: Getting Started with Xcode 4
In very short order, youll 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 to find your way around a project.
Part II: Working with Cocoa Applications
Next, youll learn how to build and edit user interfaces, add resources, and customize the application. Youll explore all major aspects of the Xcode user interface and its primary editors. Youll learn to refactor code, to use the debugger and the Core Data modeler, and to archive builds for deployment (independently or via the App Store).
Part III: Going Beyond the Basics
Then youll dive a little deeper and explore Xcodes build system (including the new schemes system). Youll learn how to create and use libraries and frameworks and how to combine multiple projects into a single workspace. Youll create and run unit tests and use custom scripts with the build process.
Finally, youll take a solid tour of Instruments (Apples profiling tool) and experience its uncanny ability to point out your mistakes and make you feel stupid. Thoroughly abashed, youll wrap up with an overview of Xcodes integrated source code management support.
Appendixes
Youll find four appendixes on the books companion Web site ( provides you with Apple and third-party resources for additional information.
Welcome to Xcode
Upstart newbies. Always strolling in and making short work of stuff that used to take you hours. In your day, you typed all your 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 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?
Interface Builder
Build and edit rich user interfaces with Interface Builder. Drag and drop outlets and actions directly into your code using the Assistant editor.