Beginning Mac Programming
Total Page:16
File Type:pdf, Size:1020Kb
Prepared exclusively for James Carlson What Readers Are Saying About Beginning Mac Programming Helping a complete novice get off the ground building real Mac (and iPhone) apps is daunting—I would have thought impossible—but Tim makes some of the toughest concepts of Cocoa accessible and enjoyable. I get tons of email from people looking to get into Cocoa programming who don’t know where to start, and there really hasn’t been a good resource to point them to until this book. Loren Brichter Owner, atebits LLC Perfect for anyone who is new to Objective-C. It is laid out well, and the examples let you jump right in to developing an application. This will be a well-thumbed reference for me for quite some time. David Flagg Internet Technology Specialist, National Science Foundation Isted’s book gently places you on the path, giving you the guided tour not just of Mac programming but of programming itself. Simple, evocative examples take care of reinforcing the theory he deftly slips into the conversation. If you want to get into Mac programming quickly, at your own pace, this is your book. Uli Kusterer Software Engineer, The Void Software A gentle, practical, and comprehensive introduction to Mac development, which is genuinely suitable for people who have never programmed before. It will not only have you building applications but also understanding how they work. Dave Verwer Director, Shiny Development Ltd. Prepared exclusively for James Carlson If you are new to programming and want to write the next killer app, this is a great place to start. Tim takes you from the very basics of programming to some fairly advanced stuff, all the while never forgetting that you are new to this. The book is filled with great explanations of not only how to do stuff but also why you should do them. I highly recommend this book to people who are new to programming. Especially if you want to build an iPhone application but have never programmed before, this book will take you through a great introduction to programming in general and leave you ready to tackle the iPhone SDK. Bill Dudney Owner, Gale Factory Software LLC This book is recommended for anyone starting out with no prior experience with programming or Cocoa development. By introducing fundamental programming principles side by side with a fantastic introduction to the Cocoa frameworks and their tools, Tim gives aspiring developers a foundation on which they can realize their goal of creating software for any modern Apple platform. Cathy Shive Senior Developer, Sofa B.V. Tim Isted clearly remembers what it was like when he first learned Objective-C, and it shows. He understands when things will be confusing and when you will be excited you got it all to work. Lyndia Zarra Owner, LIZography A book like this is all too rare to find when it comes to beginning programming literature. It focuses on results right from the get-go and has you building applications from the very start, a great way to monitor your progress. All the while it manages to teach you fundamental programming techniques in a clear, concise manner, while not sounding patronizing. An absolute must for anyone considering taking up programming on the Mac platform. Danny Greg Cocoa Developer, Realmac Software Prepared exclusively for James Carlson This book is great; it thankfully eschews the “traditional” dry format of teaching programming and takes you on a journey that will leave you itching to get on and write your own programs. Robert McGovern I’ve always been a fan of learning programming by boldly diving in. This book is a prime example of that idea: you create your first program in the second chapter, and create your first objects and code in the third. The traditional fundamentals of programming are there in the middle of the book, and an introduction to Mac application design fills out the end. But I challenge you to be bold. Play with everything, make your own marks, and this book will serve you well on the path to becoming a great Mac programmer. Matt Gallagher Sole Proprietor, ProjectsWithLove An extremely gentle introduction to the fundamentals of developing for the Mac. While some beginner books presume knowledge of arcane tidbits, this author has plotted a careful course that starts with a few essentials and then builds upon them bit by bit. Daniel Jalkut Founder, Red Sweater Software Before Isted’s book, learning how to program and learning how to program Cocoa were sadly two distinct undertakings. The best introductory programming books didn’t target Cocoa, and the Cocoa books presumed knowledge of programming. Tackling two separate books wasn’t an impossible task for a new programmer, but it was a speed bump along an already steep grade. In this book, Isted offers a seamless, gentle slope towards reaching Mac programming heights. I’m happy I can now recommend Isted’s book to those who ask me how they can start programming Apple’s lovely machines. I’m happy I can now recommend Isted’s book to those who ask me how they can start programming Apple’s lovely machines. Jonathan “Wolf” Rentzsch President, Red Shed Software Company Prepared exclusively for James Carlson Beginning Mac Programming Develop with Objective-C and Cocoa Tim Isted The Pragmatic Bookshelf Raleigh, North Carolina Dallas, Texas Prepared exclusively for James Carlson Many of the designations used by manufacturers and sellers to distinguish their prod- ucts 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 Tim Isted. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmit- ted, 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-934356-51-4 ISBN-13: 978-1-934356-51-7 Printed on acid-free paper. P1.0 printing, March 2010 Version: 2010-3-3 Prepared exclusively for James Carlson Contents 1 Introduction 10 1.1 The Intended Audience ................... 11 1.2 What’s Involved? ...................... 11 1.3 What’s Needed? ....................... 12 1.4 Acknowledgments ...................... 13 1.5 Let’sGo ............................ 14 2 Your First Application 15 2.1 Introducing Xcode ..................... 15 2.2 The Main Event ....................... 18 2.3 The Cocoa Framework ................... 22 2.4 Application Resources ................... 23 2.5 Chapter Summary ..................... 29 3 All About Objects 30 3.1 The Application Construction Process .......... 30 3.2 An Introduction to Objects ................. 31 3.3 Object Inheritance ..................... 38 3.4 Writing Code for Our Own Objects ............ 40 3.5 Chapter Summary ..................... 55 4 Object Messaging 56 4.1 Defining a New Method ................... 56 4.2 The Target-Action Mechanism ............... 59 4.3 Sending Messages from Our Code ............ 64 4.4 Chapter Summary ..................... 73 5 Variables and Memory 74 5.1 How Memory Works ..................... 74 5.2 Using Variables ....................... 79 5.3 The Scope of a Variable ................... 88 5.4 Memory Addressing ..................... 90 5.5 Pointers Again ........................ 94 5.6 Chapter Summary ..................... 96 Prepared exclusively for James Carlson CONTENTS 8 6 Passing Information Around 97 6.1 Returning Values ...................... 97 6.2 Methods and Arguments .................. 105 6.3 Class Methods ........................ 111 6.4 Passing Values by Reference ................ 115 6.5 Chapter Summary ..................... 117 7 Objects and Memory Management 119 7.1 Memory Considerations .................. 119 7.2 Allocating Memory for Objects ............... 121 7.3 Creating Objects in Code .................. 124 7.4 The Object Life Cycle .................... 129 7.5 Denying Responsibility ................... 133 7.6 Initializing with Arguments ................ 137 7.7 Utility Class Methods .................... 140 7.8 Chapter Summary ..................... 144 8 Collecting Information 145 8.1 Introducing Arrays ..................... 145 8.2 Using Arrays in an Application .............. 148 8.3 Object Mutability ...................... 154 8.4 A New Application ...................... 160 8.5 Chapter Summary ..................... 181 9 Branching Out 183 9.1 Introducing if and else ................... 183 9.2 All About the Truth ..................... 199 9.3 Stylistic Conventions .................... 202 9.4 Switching Around ...................... 205 9.5 Writing Init Methods .................... 208 9.6 Adding Conditional Statements to the Shopping List Application .......................... 210 9.7 Chapter Summary ..................... 217 10 Looping and Enumerating 218 10.1 Introducing