ptg8126863 www.it-ebooks.info ptg8126863 800 East 96th Street, Indianapolis, Indiana, 46240 USA John Ray William Ray SamsTeach Yourself 24 in Hours Xcode ® 4 www.it-ebooks.info ptg8126863 Sams Teach Yourself Xcode® 4 in 24 Hours Copyright © 2012 by Pearson Education, Inc. All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. ISBN-13: 978-0-672-33587-7 ISBN-10: 0-672-33587-5 The Library of Congress Cataloging-in-Publication data is on file. Printed in the United States of America First Printing June 2012 Trademarks All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing 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 Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk pur- chases or special sales. For more information, please contact U.S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside of the U.S., please contact International Sales international@pearsoned.com Editor-in-Chief Greg Wiegand Acquisitions Editor Laura Norman Development Editor Keith Cline Managing Editor Kristy Hart Project Editor Andy Beaster Copy Editor Keith Cline Indexer Tim Wright Proofreader Chrissy White Technical Editor Greg Kettell Publishing Coordinator Cindy Teeters Book Designer Gary Adair Compositor Nonie Ratcliff www.it-ebooks.info ptg8126863 Contents at a Glance Introduction 1 HOUR 1 Xcode 4 3 2 Just Enough Objective-C and Cocoa 23 3 Understanding the MVC Design Pattern 57 4 Using Xcode Templates to Create Projects 71 5 Managing Projects and Resources 95 6 Using the Xcode Code Source Editor 117 7 Working with the Xcode 4 Documentation 145 8 Creating User Interfaces 165 9 Connecting a GUI to Code 195 10 Creating iOS Application Workflows with Storyboarding 215 11 Building and Executing Applications 251 12 Using Source Control 279 13 Xcode-Supported Languages 311 14 Planning for Re-use: Frameworks and Libraries 329 15 Putting It All Together: Building an OS X Application 355 16 Building an iOS Application 399 17 Attaching Big Data: Using Core Data in Your Applications 435 18 Test Early, Test Often 465 19 Getting the Bugs Out 487 20 Keeping things Organized: Shared Workspaces 509 21 Advanced: Analyzing Code with Instruments 533 22 Managing and Provisioning iOS Devices 549 23 Distributing Your Applications 567 24 Xcode CLI Utilities 587 Index 603 www.it-ebooks.info ptg8126863 Table of Contents Introduction 1 HOUR 1: Xcode 4 3 Welcome to Xcode 3 The Apple Developer Programs 8 Installing the Xcode Developer Tools 11 The Nickel Tour 13 Preparing Your iOS Device (Optional) 17 Summary 20 Q&A 20 Workshop 21 HOUR 2: Just Enough Objective-C and Cocoa 23 Object-Oriented Programming and Objective-C 23 What Is Objective-C? 25 Objective-C Programming Basics 38 Memory Management and Automatic Reference Counting 48 What Is Cocoa? 50 Cocoa Versus Cocoa Touch 51 Summary 54 Q&A 55 Workshop 55 HOUR 3: Understanding the MVC Design Pattern 57 Development, Design Patterns, and MVC 57 How Xcode Implements MVC 60 An MVC Walkthrough 64 Summary 68 Q&A 68 Workshop 69 www.it-ebooks.info ptg8126863 HOUR 4: Using Xcode Templates to Create Projects 71 Available Project Types 71 The Project-Creation Process 83 You’ve Got a Project 88 Summary 91 Q&A 91 Workshop 92 HOUR 5: Managing Projects and Resources 95 Getting Your Bearings 95 Managing Project Files 99 Managing Frameworks and Libraries 108 Managing Groups 110 Managing Target Properties 111 Summary 114 Q&A 115 Workshop 115 HOUR 6: Using the Xcode Source Editor 117 Understanding Editor Basics 117 Navigating Within and Between Files 126 Using the Assistant Editor 131 Correcting Errors and Warnings in the Issue Navigator 133 Refactoring Code 135 Using Code Snippets 138 Summary 142 Q&A 142 Workshop 142 HOUR 7: Working with the Xcode 4 Documentation 145 Overview of Documentation Resources 145 Configuring the Xcode Documentation Downloads 148 Understanding the Documentation Resources 149 Using the Xcode Help Viewer 155 Table of Contents v www.it-ebooks.info ptg8126863 Using the Quick Help Assistant 158 Summary 161 Q&A 162 Workshop 162 HOUR 8: Creating User Interfaces 165 What Is Interface Builder? 165 The Anatomy of an Interface Builder File 168 Creating User Interfaces 174 Working with the IB Layout Tools 177 Customizing Interface Appearance 188 Setting Object Identities 191 Adding Custom Objects to Interface Builder 192 Summary 193 Q&A 193 Workshop 194 HOUR 9: Connecting a GUI to Code 195 Outlet, Actions, and Properties: A Review 195 Making Connections to Outlets and Actions 197 Writing Connection Code with Interface Builder 205 Summary 211 Q&A 212 Workshop 212 HOUR 10: Creating iOS Application Workflows with Storyboards 215 The Power of Storyboards 215 Storyboard Terminology 216 The Anatomy of a Multiscene Project 218 Making Advanced Segues 230 A Navigation Storyboard Example 239 Summary 248 Q&A 249 Workshop 249 vi Sams Teach Yourself Xcode 4 in 24 Hours www.it-ebooks.info ptg8126863 HOUR 11: Building and Executing Applications 251 The Language of the Build 251 Managing Targets 257 Managing Schemes 264 Using the iOS Simulator 271 Summary 276 Q&A 277 Workshop 277 HOUR 12: Using Source Control 279 Using Xcode Snapshots 279 A Brief Introduction to Source Control Systems 283 Working with Subversion and Git Repositories 285 Managing a Project in Source Control 296 Summary 307 Q&A 308 Workshop 308 HOUR 13: Xcode-Supported Languages 311 Choosing the Right Language 312 Built-In Languages 313 Adding Support for Other Languages 322 Summary 325 Q&A 326 Workshop 326 HOUR 14: Planning for Reuse: Frameworks and Libraries 329 Understanding Frameworks 329 Deploying a Framework 348 Reusing Code from Existing C/C++ Libraries 349 Summary 352 Q&A 352 Workshop 353 Table of Contents vii www.it-ebooks.info ptg8126863 HOUR 15: Putting It All Together: Building an OS X Application 355 Getting Started 356 Creating the Interface 359 Attaching Code 364 Inserting Interface Object References into the Code 369 Tying Things Together 373 Increasing Functionality with a Framework 376 Summary 395 Q&A 396 Workshop 396 HOUR 16: Building an iOS Application 399 Assessing What You Already Have 400 Building from the Template 403 Adding a Static Library Target 404 Updating Application Logic and Library Calls for iOS 414 Summary 430 Q&A 431 Workshop 432 HOUR 17: Attaching Big Data: Using Core Data in Your Applications 435 Introducing Core Data 435 Using the Xcode Core Data Model Editor 438 Binding a Data Model to a User Interface 446 Accessing Data Through Code 458 Summary 462 Q&A 463 Workshop 463 HOUR 18: Test Early, Test Often 465 Adding Unit Tests to an Existing Application 466 Implementing Tests for Existing Code 472 Accessing the Rest of an Application Through the Bundle Loader 480 viii Sams Teach Yourself Xcode 4 in 24 Hours www.it-ebooks.info ptg8126863 Summary 483 Q&A 484 Workshop 485 HOUR 19: Getting the Bugs Out 487 Getting Started with the Debugger 488 Proactive Debugging 493 Working with Breakpoints 497 Summary 506 Q&A 506 Workshop 507 HOUR 20: Keeping Things Organized: Shared Workspaces 509 Using Workspaces 510 Creating a Workspace 515 Adding Projects to the Workspace 517 Configuring the OS X Project to Work in the Workspace 519 Configuring the iOS Project to Work in the Workspace 524 Summary 530 Q&A 531 Workshop 531 HOUR 21: Advanced: Analyzing Code with Instruments 533 The Instruments Interface 534 Using Instruments 536 Additional Runs 541 Collecting Data from Additional Instruments Simultaneously 543 Summary 545 Q&A 546 Workshop 546 HOUR 22: Managing and Provisioning iOS Devices 549 Creating an iOS Distribution Certificate 550 Creating an App ID 556 Table of Contents ix www.it-ebooks.info [...]... 602 Index 603 www.it-ebooks.info About the Authors John Ray is currently serving as a Senior Business Analyst and Development Team Manager for the Ohio State University Office of Research He has written numerous books for Macmillan /Sams/ Que, including Using TCP/IP: Special Edition, Teach Yourself Dreamweaver MX in 21 Days, Mac OS X Unleashed, and Teach Yourself iOS 5 Development in 24 Hours As a Macintosh... targets individuals who have used programming tools but who are new to the Xcode development platform Although no previous development experience is required to complete the book, an understanding of programming fundamentals is helpful To be clear, www.it-ebooks.info 2 Sams Teach Yourself Xcode 4 in 24 Hours even though we provide code samples and an introduction to Objective-C, we do not have the space in. .. Differences from Xcode 3 The Xcode interface basics Benefits of being a paid developer What to expect during the first few hours of this book Do you love using your Mac or iOS device? If so, you can thank Xcode Xcode is the starting point for nearly all the applications you know and love on your favorite operating system It contains the tools for writing code, developing interfaces, testing performance,...x Sams Teach Yourself Xcode 4 in 24 Hours Creating a Distribution Provisioning Profile 560 563 5 64 Summary Q&A Workshop 565 HOUR 23: Distributing Your Applications 567 Finalizing Your Distribution Build 568 Configuring an iTunes... UNIX technologies since 1989 You can visit their Xcode book website at http://teachyourselfxcode.com or follow their book-related tweets on Twitter at #XcodeIn 24 www.it-ebooks.info Dedication Since Will and I couldn’t agree on dedicating this to his parents or mine, we hereby dedicate this book to the game #Starhawk Come play the authors and discuss Xcode in the regularly appearing “Old-N-Slow” server... http://teachyourselfxcode.com If you have issues with any projects, view the posts on this site to see whether a solution has been posted In addition to the support website, you can follow along on Twitter Search for #XcodeIn 24 on Twitter to receive official updates and tweets from other readers Use the hashtag #XcodeIn 24 in your tweets to join the conversation To send me messages via Twitter, begin each... pediatrics in the Battelle Center for Mathematical Medicine at Nationwide Children’s Hospital Trained as a biophysicist in computational biology and scientific visualization, Dr Ray’s group is working to bring cutting-edge computational technology to end users, through simplified user interfaces He has been developing training materials and teaching users and programmers to live at the intersection of Macintosh... presentation from SecondConf in 2010: http://cdn.secondconf.com/2010/videos/SecondConfGeneBacklin-1 742 5.mp4 Some of the biggest changes between Xcode 3 and Xcode 4 include the following: A single-window unified development environment Instant-access editors and viewers for code files, interfaces, data models, and more Detailed code analysis and error detection Integrated interface builder Updated... the software by going to http://developer.apple.com/, choosing the developer program you enrolled in (iOS or Mac), and then logging in to access a direct-download installer for Xcode www.it-ebooks.info 12 HOUR 1: Xcode 4 FIGURE 1.8 Download Xcode from the Mac App Store By the Way If you have the free developer membership and log in to the Dev Center, you see just a single installer for Xcode If you’ve... development tools are contained within a single window If you’re used to Xcode 3.x, this will come as a bit of a shock You have much less flexibility in how you arrange your tools However, the consistency in the interface makes it easy to switch between different editing modes and jump between interface, code, and back without losing your focus www.it-ebooks.info 16 HOUR 1: Xcode 4 The Xcode IDE consists of . ptg8126863 www.it-ebooks.info ptg8126863 800 East 96th Street, Indianapolis, Indiana, 46 240 USA John Ray William Ray SamsTeach Yourself 24 in Hours Xcode ® 4 www.it-ebooks.info ptg8126863 Sams Teach Yourself Xcode . Terminology 216 The Anatomy of a Multiscene Project 218 Making Advanced Segues 230 A Navigation Storyboard Example 239 Summary 248 Q&A 249 Workshop 249 vi Sams Teach Yourself Xcode 4 in 24 Hours www.it-ebooks.info ptg8126863 HOUR. Applications 43 5 Introducing Core Data 43 5 Using the Xcode Core Data Model Editor 43 8 Binding a Data Model to a User Interface 44 6 Accessing Data Through Code 45 8 Summary 46 2 Q&A 46 3 Workshop 46 3 HOUR