ebook img

Objective-C for Absolute Beginners: iPhone, iPad and Mac Programming Made Easy PDF

284 Pages·2010·13.99 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Objective-C for Absolute Beginners: iPhone, iPad and Mac Programming Made Easy

LLeeaarrnn OObbjjeecctt--OOrriieenntteedd PPrrooggaammmmiinngg wwiitthh OObbjjeeccttiivvee--CC ffoorr iiOOSS uussiinngg AAlliiccee OObbjjeeccttiivvee--CC ffoorr AAbbssoolluuttee BBeeggiinnnneerrss iiPPhhoonnee ,, iiPPaadd MMaacc PPrrooggrraammmmiinngg MMaaddee EEaassyy aanndd GGaarryy BBeennnneetttt || MMiittcchh FFiisshheerr || BBrraadd LLeeeess Objective-C for Absolute Beginners iPhone, iPad, and Mac Programming Made Easy ■ ■ ■ Gary Bennett Mitch Fisher Brad Lees i Objective-C for Absolute Beginners: iPhone, iPad, and Mac Programming Made Easy Copyright © 2010 by Gary Bennett, Mitch Fisher, Brad Lees All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2832-5 ISBN-13 (electronic): 978-1-4302-2833-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Clay Andres Development Editor: Douglas Pundick Technical Reviewer: James Bucanek Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Kelly Moritz Copy Editor: Heather Lang and Tracy Brown Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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 information contained in this work. The source code for this book is available to readers at www.apress.com. ii Contents at a Glance ■Contents at a Glance.......................................................................................iv ■Contents..........................................................................................................v ■About the Authors ...........................................................................................x ■About the Technical Reviewer........................................................................xi ■Acknowledgments.........................................................................................xii ■Introduction..................................................................................................xiii ■Chapter 1: Becoming a Great iPhone/iPad or Mac Programmer.....................1(cid:1) ■Chapter 2: Programming Basics...................................................................13(cid:1) ■Chapter 3: It’s All About the Data..................................................................37(cid:1) ■Chapter 4: Making Decisions About…and Planning Program Flow..............57(cid:1) ■Chapter 5: Object Oriented Programming with Objective-C..........................81(cid:1) ■Chapter 6: Introducing Objective-C and Xcode..............................................97(cid:1) ■Chapter 7: Objective-C Classes, Objects, and Methods...............................117(cid:1) ■Chapter 8: Programming Basics in Objective-C..........................................137(cid:1) ■Chapter 9: Comparing Data.........................................................................157(cid:1) ■Chapter 10: Creating User Interfaces with Interface Builder......................175(cid:1) ■Chapter 11: Memory, Addresses, and Pointers...........................................199(cid:1) ■Chapter 12: Debugging Programs with Xcode............................................219(cid:1) ■Chapter 13: Storing Information..................................................................237(cid:1) ■Chapter 14: Protocols and Delegates..........................................................257(cid:1) ■Index............................................................................................................263 iv Contents ■Contents at a Glance.......................................................................................iv(cid:1) ■Contents..........................................................................................................v(cid:1) ■About the Authors ...........................................................................................x(cid:1) ■About the Technical Reviewer........................................................................xi(cid:1) ■Acknowledgments.........................................................................................xii ■Introduction..................................................................................................xiii(cid:1) ■Chapter 1: Becoming a Great iPhone/iPad or Mac Programmer.....................1(cid:1) Thinking Like a Developer......................................................................................................................................1(cid:1) Completing the Development Cycle........................................................................................................................4(cid:1) Introducing Object Oriented Programming.............................................................................................................6(cid:1) Working with the Alice Interface.............................................................................................................................8(cid:1) Summary..............................................................................................................................................................11(cid:1) Exercises...............................................................................................................................................................11(cid:1) ■Chapter 2: Programming Basics...................................................................13(cid:1) Taking a Tour with Alice.......................................................................................................................................13(cid:1) Navigation Menu..............................................................................................................................................14(cid:1) World Window..................................................................................................................................................15(cid:1) Classes, Objects, and Instances in Alice..........................................................................................................17(cid:1) Object Tree.......................................................................................................................................................18(cid:1) Editor Area.......................................................................................................................................................18(cid:1) Details Area......................................................................................................................................................19(cid:1) Events Area......................................................................................................................................................19(cid:1) Creating an Alice App—To the Moon Alice...........................................................................................................20(cid:1) Your First Objective-C Program............................................................................................................................26(cid:1) Installing Xcode...............................................................................................................................................27(cid:1) Launching and Using Xcode.............................................................................................................................30(cid:1) Summary..............................................................................................................................................................35(cid:1) Exercises...............................................................................................................................................................36(cid:1) v ■ CONTENTS ■Chapter 3: It’s All About the Data..................................................................37(cid:1) Numbering Systems Used in Programming..........................................................................................................37(cid:1) Bits...................................................................................................................................................................37(cid:1) Bytes................................................................................................................................................................39(cid:1) Hexadecimal....................................................................................................................................................41(cid:1) Unicode............................................................................................................................................................42(cid:1) Data Types............................................................................................................................................................42(cid:1) Using Variable and Data Types with Alice.............................................................................................................43(cid:1) Data Types and Objective-C..................................................................................................................................50(cid:1) Identifying Problems.............................................................................................................................................54(cid:1) Summary..............................................................................................................................................................56(cid:1) Exercises...............................................................................................................................................................56(cid:1) ■Chapter 4: Making Decisions About…and Planning Program Flow..............57(cid:1) Boolean Logic.......................................................................................................................................................57(cid:1) Truth Tables.....................................................................................................................................................59(cid:1) Comparison Operators.....................................................................................................................................61(cid:1) Designing Apps.....................................................................................................................................................62(cid:1) Pseudo-code....................................................................................................................................................62(cid:1) Design Requirements............................................................................................................................................64(cid:1) Flowcharting....................................................................................................................................................67(cid:1) Designing and Flowcharting an Example App.................................................................................................68(cid:1) The App’s Design.............................................................................................................................................69(cid:1) Using Loops to Repeat Program Statements...................................................................................................70(cid:1) Coding the Example App in Alice..........................................................................................................................72(cid:1) Coding the Example App in Objective-C................................................................................................................74(cid:1) Nested If Statements and Else-If Statements.......................................................................................................77(cid:1) Improving the Code Through Refactoring.............................................................................................................77(cid:1) Moving Forward Without Alice..............................................................................................................................78(cid:1) Summary..............................................................................................................................................................79(cid:1) Exercises...............................................................................................................................................................80(cid:1) ■Chapter 5: Object Oriented Programming with Objective-C..........................81(cid:1) The Object.............................................................................................................................................................81(cid:1) What Is a Class.....................................................................................................................................................82(cid:1) Planning Classes...................................................................................................................................................83(cid:1) Inheritance............................................................................................................................................................93(cid:1) Why Use OOP?......................................................................................................................................................94(cid:1) Eliminate Redundant Code...............................................................................................................................94(cid:1) Ease of Debugging...........................................................................................................................................95(cid:1) Ease of Replacement.......................................................................................................................................95(cid:1) Advanced Topics...................................................................................................................................................95(cid:1) Interface...........................................................................................................................................................95(cid:1) Polymorphism..................................................................................................................................................95(cid:1) Summary..............................................................................................................................................................96(cid:1) Exercises...............................................................................................................................................................96(cid:1) ■Chapter 6: Introducing Objective-C and Xcode..............................................97(cid:1) A Brief History of Objective-C...............................................................................................................................97(cid:1) Understanding C Language Basics.......................................................................................................................98(cid:1) vi ■ CONTENTS Putting the “Objective” into Objective-C.............................................................................................................100(cid:1) Introducing Xcode...............................................................................................................................................105(cid:1) Starting Up Xcode...............................................................................................................................................106(cid:1) Creating Your First Project..................................................................................................................................107(cid:1) Adding a New Class.......................................................................................................................................108(cid:1) Building and Running the New Program........................................................................................................114(cid:1) Summary............................................................................................................................................................115(cid:1) Exercises.............................................................................................................................................................116(cid:1) ■Chapter 7: Objective-C Classes, Objects, and Methods...............................117(cid:1) Creating an Objective-C Class............................................................................................................................117(cid:1) Declaring Interfaces and Instance Variables.................................................................................................119(cid:1) Sending Messages (Methods)........................................................................................................................119(cid:1) Working with the Implementation File...........................................................................................................121(cid:1) Implementing Methods..................................................................................................................................123(cid:1) Using Our New Class...........................................................................................................................................125(cid:1) Overriding Default Behavior...........................................................................................................................132(cid:1) Taking Class Methods to the Next Level........................................................................................................133(cid:1) Accessing the Xcode Documentation.................................................................................................................133(cid:1) Summary............................................................................................................................................................134(cid:1) Exercises.............................................................................................................................................................135(cid:1) ■Chapter 8: Programming Basics in Objective-C..........................................137(cid:1) Creating a Simple Command Line Tool...............................................................................................................138(cid:1) Introducing Instance Variables...........................................................................................................................140(cid:1) Accessing Instance Variables........................................................................................................................141(cid:1) Using Getter and Setter Methods...................................................................................................................142(cid:1) Introducing Properties........................................................................................................................................144(cid:1) Using Properties.............................................................................................................................................145(cid:1) Understanding the Importance of Conventions..............................................................................................146(cid:1) Creating the MyBookstore Program....................................................................................................................146(cid:1) Using the NSMutableDictionary Class............................................................................................................148(cid:1) Making Our Object Do Something..................................................................................................................149(cid:1) Implementing Behavior..................................................................................................................................150(cid:1) Cleaning Up Our Objects................................................................................................................................153(cid:1) Using the Bookstore and Book Objects..........................................................................................................154(cid:1) Summary............................................................................................................................................................156(cid:1) Exercises.............................................................................................................................................................156(cid:1) ■Chapter 9: Comparing Data.........................................................................157(cid:1) Introducing Boolean Logic..................................................................................................................................157(cid:1) Using Relational Operators.................................................................................................................................158(cid:1) Comparing Numbers......................................................................................................................................158(cid:1) Using Boolean Expressions.................................................................................................................................163(cid:1) Comparing Strings.........................................................................................................................................164(cid:1) Comparing Dates...........................................................................................................................................166(cid:1) Combining Comparisons................................................................................................................................168(cid:1) Using the Switch Statement...............................................................................................................................168(cid:1) Grouping Variables Together..............................................................................................................................170(cid:1) NSArray..........................................................................................................................................................170(cid:1) vii ■ CONTENTS NSMutableArray.............................................................................................................................................171(cid:1) NSDictionary..................................................................................................................................................172(cid:1) NSMutableDictionary.....................................................................................................................................172(cid:1) Summary............................................................................................................................................................173(cid:1) Exercises.............................................................................................................................................................174(cid:1) ■Chapter 10: Creating User Interfaces with Interface Builder......................175(cid:1) Understanding Interface Builder.........................................................................................................................176(cid:1) The Model-View-Controller.................................................................................................................................177(cid:1) Human Interface Guidelines (HIGs).....................................................................................................................179(cid:1) Creating an Example iPhone App with Interface Builder....................................................................................180(cid:1) Using Outlets..................................................................................................................................................185(cid:1) Implementing an Action.................................................................................................................................186(cid:1) Using Interface Builder..................................................................................................................................187(cid:1) Document Window.........................................................................................................................................188(cid:1) Library Window..............................................................................................................................................189(cid:1) Inspector Window..........................................................................................................................................190(cid:1) Creating the View...........................................................................................................................................191(cid:1) Connecting the Outlets and Objects...............................................................................................................192(cid:1) Connecting Actions and Objects....................................................................................................................193(cid:1) Implementation File.......................................................................................................................................195(cid:1) Broken Connections in Interface Builder............................................................................................................196(cid:1) Summary............................................................................................................................................................198(cid:1) Exercises.............................................................................................................................................................198(cid:1) ■Chapter 11: Memory, Addresses, and Pointers...........................................199(cid:1) Understanding Memory.......................................................................................................................................200(cid:1) Bits, Bytes, and Bases...................................................................................................................................200(cid:1) Understanding Memory Address Basics.............................................................................................................204(cid:1) Requesting Memory............................................................................................................................................208(cid:1) Working with Automatic Variables and Pointers............................................................................................208(cid:1) Deallocating Memory..........................................................................................................................................209(cid:1) Using Special Pointers...................................................................................................................................210(cid:1) Managing Memory in Objective-C.......................................................................................................................212(cid:1) Using the Retain/Release Model....................................................................................................................212(cid:1) Working with Implied Retain Messages.........................................................................................................214(cid:1) Sending the dealloc Message........................................................................................................................215(cid:1) If Things Go Wrong..............................................................................................................................................216(cid:1) Summary............................................................................................................................................................217(cid:1) Exercises.............................................................................................................................................................218(cid:1) ■Chapter 12: Debugging Programs with Xcode............................................219(cid:1) Getting Started with Debugging..........................................................................................................................220(cid:1) Setting Breakpoints.......................................................................................................................................220(cid:1) Debugging Basics..........................................................................................................................................222(cid:1) Working with the Debugger Controls.............................................................................................................223(cid:1) Debugging a Program.........................................................................................................................................224(cid:1) Using the Step Controls.................................................................................................................................225(cid:1) Looking at the Thread Window and Call Stack..............................................................................................225(cid:1) Debugging Variables......................................................................................................................................226(cid:1) viii ■ CONTENTS Deleting Multiple Breakpoints........................................................................................................................230(cid:1) Disabling Breakpoints....................................................................................................................................231(cid:1) A Larger Call Stack........................................................................................................................................231(cid:1) Summary............................................................................................................................................................234(cid:1) Exercises.............................................................................................................................................................235(cid:1) ■Chapter 13: Storing Information..................................................................237(cid:1) Storage Considerations.......................................................................................................................................237(cid:1) Preferences.........................................................................................................................................................237(cid:1) Writing Preferences.......................................................................................................................................238(cid:1) Reading Preferences......................................................................................................................................239(cid:1) Databases...........................................................................................................................................................239(cid:1) Storing Information in a Database......................................................................................................................240(cid:1) Getting Started with Core Data...........................................................................................................................241(cid:1) The Model...........................................................................................................................................................242(cid:1) Managed Object Context................................................................................................................................250(cid:1) Setting Up the Interface......................................................................................................................................250(cid:1) Summary............................................................................................................................................................255(cid:1) Exercises.............................................................................................................................................................255(cid:1) ■Chapter 14: Protocols and Delegates..........................................................257(cid:1) Multiple Inheritance............................................................................................................................................257(cid:1) Understanding Protocols.....................................................................................................................................258(cid:1) Protocol Syntax..............................................................................................................................................259(cid:1) Understanding Delegates....................................................................................................................................259(cid:1) Next Steps...........................................................................................................................................................260 Summary............................................................................................................................................................261(cid:1) ■Index............................................................................................................263 ix ■ INTRODUCTION About the Authors Gary Bennett is president of xcelMe.com. xcelMe teaches iPhone/iPad programming courses online. Gary has taught hundreds of students how to develop iPhone/iPad apps, and has several very popular apps on the iTunes Apps Store. Gary’s students have some of the best-selling apps on the iTunes App Store. Gary also worked for 25 years in the technology and defense industries. He served 10 years in the U.S. Navy as a Nuclear Engineer aboard two nuclear submarines. After leaving the Navy, Gary worked for several companies as a software developer, CIO, and President. As CIO, he helped take VistaCare public in 2002. Gary also co- authored iPhone Cool Projects for Apress. Gary lives in Scottsdale, Arizona with his wife Stefanie and their four children. Mitch Fisher is a software developer in the Phoenix, Arizona area. He was introduced to PCs back in the 1980s when 64K was a lot of memory and 1 Mhz was considered a fast computer. Over the last 25 years, Mitch has worked for several large and medium-sized companies in the roles of software developer and software architect, and had led several teams of developers on multi-million dollar projects. Mitch now divides his time between writing iOS applications and server-side UNIX technologies. Brad Lees has more than 12 years’ experience in application development and server management. He has specialized in creating and initiating software programs in real-estate development systems and financial institutions. His career has been highlighted by his positions as information systems manager at The Lyle Anderson Company; product development manager for Smarsh; vice president of application development for iNation; and IT manager at The Orcutt/Winslow Partnership, the largest architectural firm in Arizona. A graduate of Arizona State University, Brad and his wife Natalie reside in Phoenix with their five children x

Description:
It seems as if everyone is writing applications for Apple’s iPhone and iPad, but how do they all do it? It’s best to learn Objective-C, the native language of both the iOSand Mac OS X, but where to begin? Right here, even if you’ve never programmed before! Objective-C for Absolute Beginners wi
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.