ebook img

Mac Programming for Absolute Beginners PDF

415 Pages·2011·10.96 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 Mac Programming for Absolute Beginners

BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Get started with Objective-C,Cocoa, Want to learn how to program on your Mac? Not sure where to begin? and Xcode on the Mac Best-selling author Wallace Wang will explain how to get started with Cocoa, Objective-C, and Xcode. Whether you are an experienced Windows coder moving to the Mac or you are completely new to programming, you’ll see how the basic design of a Mac OS X program works, how Objective-C differs from other languages you may have used, and how to use the Xcode development environment. Most importantly, you’ll learn how to use ele- ments of the Cocoa framework to create windows, store data, and respond to users in your own Mac programs. If you want to learn how to develop apps with Cocoa, Objective-C, and Xcode, this book is a great first step to getting started. Here are just a few of the things you’ll master along the way: • Fundamental programming concepts aided by short, easy-to-understand examples • How to use Xcode and related programming tools to save time and work more efficiently • A firm understanding of the basics of Objective-C and how it compares to other languages you might know • How to create simple apps using the Cocoa framework • How to easily design, write, test, and market your finished program With this book and your trusty Mac, you’re well on your way to transforming your Mac app ideas into real applications. Mac Programming Absolute Beginners for Wallace Wang www.it-ebooks.info Mac Programming for Absolute Beginners ■ ■ ■ Wallace Wang i www.it-ebooks.info Mac Programming for Absolute Beginners Copyright © 2011 by Wallace Wang 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-3336-7 ISBN-13 (electronic): 978-1-4302-3337-4 Printed and bound in the United States of America (POD) 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: Michelle Lowman Development Editor: Jim Markham Technical Reviewer: James Bucanek Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jennifer L. Blackwell Copy Editors: Kim Wimpsett and Bill McManus 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 www.it-ebooks.info This book is dedicated to all those dreamers who just needed a helping hand to turn their great ideas into a working program. iii www.it-ebooks.info Contents at a Glance ■Contents .......................................................................................................... v ■About the Author ........................................................................................... xii ■About the Technical Reviewer ...................................................................... xiii ■Acknowledgments ......................................................................................... xiv ■Introduction .................................................................................................... xv ■Chapter 1: Understanding Programming ......................................................... 1(cid:1) ■Chapter 2: Understanding Apple’s Programming Tools ................................ 17(cid:1) ■Chapter 3: The Basic Steps to Creating a Mac Program ............................... 29(cid:1) ■Chapter 4: Getting Help ................................................................................. 47(cid:1) ■Chapter 5: Learning Objective-C .................................................................... 63(cid:1) ■Chapter 6: Making Decisions with Branches ................................................. 83(cid:1) ■Chapter 7: Repeating Code with Loops .......................................................... 99(cid:1) ■Chapter 8: Understanding the Cocoa Framework ........................................ 111(cid:1) ■Chapter 9: Manipulating Strings ................................................................. 123(cid:1) ■Chapter 10: Arrays ...................................................................................... 139(cid:1) ■Chapter 11: Dictionaries and Sets ............................................................... 157(cid:1) ■Chapter 12: Creating Classes and Objects .................................................. 173(cid:1) ■Chapter 13: Inheritance, Method Overriding, and Events ............................ 203(cid:1) ■Chapter 14: Creating a User Interface ......................................................... 215(cid:1) ■Chapter 15: Choosing Commands with Buttons .......................................... 231(cid:1) ■Chapter 16: Making Choices with Radio Buttons and Check Boxes ............ 249(cid:1) ■Chapter 17: Making Choices with Pop-Up Buttons ...................................... 263(cid:1) ■Chapter 18: Inputting and Outputting Data with Labels, Text Fields, and Combo Boxes ................................................. 279(cid:1) ■Chapter 19: Inputting Data with Sliders, Date Pickers, and Steppers ......... 299(cid:1) ■Chapter 20: Using Built-In Dialog Boxes ..................................................... 315(cid:1) ■Chapter 21: Creating Pull-Down Menus ...................................................... 331(cid:1) ■Chapter 22: Designing Your Own Programs ................................................ 343(cid:1) ■Chapter 23: Working with Xcode ................................................................. 361(cid:1) ■Chapter 24: Debugging Your Program ......................................................... 371 ■Index ............................................................................................................ 385 iv www.it-ebooks.info Contents ■Contents at a Glance ....................................................................................... iv(cid:1) ■About the Author ........................................................................................... xii(cid:1) ■About the Technical Reviewer ...................................................................... xiii(cid:1) ■Acknowledgments ......................................................................................... xiv(cid:1) ■Introduction .................................................................................................... xv(cid:1) ■Chapter 1: Understanding Programming ......................................................... 1(cid:1) Programming Principles ......................................................................................................................................... 2(cid:1) Dividing Programs into Parts ............................................................................................................................. 5(cid:1) Event-Driven Programming ................................................................................................................................ 7(cid:1) Object-Oriented Programming ........................................................................................................................... 8(cid:1) Understanding Programming Languages ............................................................................................................. 11(cid:1) The Building Blocks of Programming Languages ............................................................................................ 12(cid:1) Programming Frameworks ............................................................................................................................. 13(cid:1) Mac Programming Today ................................................................................................................................. 14(cid:1) Summary .............................................................................................................................................................. 16(cid:1) ■Chapter 2: Understanding Apple’s Programming Tools ................................ 17(cid:1) Understanding Editors .......................................................................................................................................... 17(cid:1) Understanding Xcode ............................................................................................................................................ 18(cid:1) Deciphering the Xcode User Interface ............................................................................................................. 19(cid:1) Running Xcode ................................................................................................................................................. 20(cid:1) Creating a New Project in Xcode ..................................................................................................................... 21(cid:1) Examining Project Files in Xcode ..................................................................................................................... 24(cid:1) Compiling a Program ....................................................................................................................................... 26(cid:1) Summary .............................................................................................................................................................. 27(cid:1) ■Chapter 3: The Basic Steps to Creating a Mac Program ............................... 29(cid:1) A Bare-Bones Program Example ........................................................................................................................... 30(cid:1) A Simple User Interface Example .......................................................................................................................... 33(cid:1) An Interactive User Interface Example .................................................................................................................. 37(cid:1) Writing Objective-C Code ................................................................................................................................. 38(cid:1) Connecting the User Interface ......................................................................................................................... 40 v www.it-ebooks.info ■ CONTENTS An Advanced Interactive User Interface Example ................................................................................................. 42(cid:1) Summary .............................................................................................................................................................. 45(cid:1) ■Chapter 4: Getting Help ................................................................................. 47(cid:1) Installing Help Topics ............................................................................................................................................ 47(cid:1) Getting Help About Xcode ..................................................................................................................................... 48(cid:1) Getting Help About Core Library ............................................................................................................................ 49(cid:1) Searching for Help ................................................................................................................................................ 54(cid:1) Getting Quick Help ................................................................................................................................................ 55(cid:1) Viewing Documentation for Selected Text ............................................................................................................ 56(cid:1) Getting Help with Library Windows ....................................................................................................................... 57(cid:1) Help While Writing Code ....................................................................................................................................... 59(cid:1) Color-Coding .................................................................................................................................................... 59(cid:1) Customizing the Editor ..................................................................................................................................... 60(cid:1) Using Code Completion .................................................................................................................................... 60(cid:1) Summary .............................................................................................................................................................. 61(cid:1) ■Chapter 5: Learning Objective-C .................................................................... 63(cid:1) Differences in Writing a Mac Objective-C Program .............................................................................................. 63(cid:1) Understanding Objective-C Symbols .................................................................................................................... 65(cid:1) Defining the End of Each Line with a Semicolon ............................................................................................. 66(cid:1) Defining the Beginning and End of Code with Curly Brackets ......................................................................... 67(cid:1) Defining Compiler Directives with the # Symbol ............................................................................................. 68(cid:1) Defining Comments with // .............................................................................................................................. 68(cid:1) Identifying Objects with [ and ] ........................................................................................................................ 69(cid:1) Defining Pointers with * ................................................................................................................................... 70(cid:1) Manipulating Data with Variables ......................................................................................................................... 71(cid:1) Declaring Variables .......................................................................................................................................... 71(cid:1) Assigning Data to a Variable ............................................................................................................................ 73(cid:1) The Scope of a Variable ................................................................................................................................... 73(cid:1) A Program Example Using Variables ................................................................................................................ 75(cid:1) Using Constants .................................................................................................................................................... 76(cid:1) Using Mathematical Operators ............................................................................................................................. 78(cid:1) Using Strings ......................................................................................................................................................... 79(cid:1) Summary .............................................................................................................................................................. 81(cid:1) ■Chapter 6: Making Decisions with Branches ................................................. 83(cid:1) Understanding Boolean Expressions .................................................................................................................... 84(cid:1) Boolean Comparison Operators ....................................................................................................................... 86(cid:1) Boolean Logical Operators ............................................................................................................................... 87(cid:1) Branches ............................................................................................................................................................... 90(cid:1) The Simplest if Statement ............................................................................................................................... 90(cid:1) Following Multiple Instructions in an if Statement .......................................................................................... 91(cid:1) The if-else Statement ...................................................................................................................................... 92(cid:1) The if-else if Statement ................................................................................................................................... 92(cid:1) The switch Statement ...................................................................................................................................... 94(cid:1) Summary .............................................................................................................................................................. 98(cid:1) ■Chapter 7: Repeating Code with Loops .......................................................... 99(cid:1) Loops That Run a Fixed Number of Times .......................................................................................................... 100(cid:1) Quitting a for Loop Prematurely ..................................................................................................................... 102(cid:1) vi www.it-ebooks.info ■ CONTENTS Skipping in a for Loop .................................................................................................................................... 103(cid:1) Loops That Run Zero or More Times ................................................................................................................... 104(cid:1) The while Loop ............................................................................................................................................... 104(cid:1) The do-while Loop ......................................................................................................................................... 105(cid:1) Quitting a while or do-while Loop Prematurely ............................................................................................. 106(cid:1) Skipping a while or do-while Loop ................................................................................................................ 107(cid:1) Nested Loops ...................................................................................................................................................... 107(cid:1) Summary ............................................................................................................................................................ 109(cid:1) ■Chapter 8: Understanding the Cocoa Framework ........................................ 111(cid:1) An Overview of How Object-Oriented Programming Works ................................................................................ 112(cid:1) Starting with a Class ...................................................................................................................................... 113(cid:1) Reducing Bugs ............................................................................................................................................... 114(cid:1) Reusing Code ................................................................................................................................................. 114(cid:1) Defining Classes ............................................................................................................................................ 114(cid:1) Creating an Object .............................................................................................................................................. 115(cid:1) Storing Data in an Object .................................................................................................................................... 116(cid:1) A Sample Program for Manipulating Objects ...................................................................................................... 117(cid:1) Looking Up Method and Property Names for NS Classes ................................................................................... 118(cid:1) Summary ............................................................................................................................................................ 121(cid:1) ■Chapter 9: Manipulating Strings ................................................................. 123(cid:1) Declaring a String Variable ................................................................................................................................. 123(cid:1) Getting the Length of a String ............................................................................................................................. 124(cid:1) Comparing Two Strings ...................................................................................................................................... 125(cid:1) Checking for Prefixes and Suffixes ..................................................................................................................... 125(cid:1) Converting to Uppercase and Lowercase ........................................................................................................... 126(cid:1) Converting Strings to Numbers ........................................................................................................................... 127(cid:1) Searching for a Substring ................................................................................................................................... 129(cid:1) The location Field ........................................................................................................................................... 129(cid:1) The length Field ............................................................................................................................................. 129(cid:1) Searching and Replacing .................................................................................................................................... 130(cid:1) Replacing Part of a String at a Specific Location ........................................................................................... 130(cid:1) Searching for and Replacing Part of a String ................................................................................................ 132(cid:1) Deleting Part of a String ...................................................................................................................................... 133(cid:1) Extracting a Substring ........................................................................................................................................ 134(cid:1) Extracting a Substring with a Location and Length ....................................................................................... 134(cid:1) Extracting a Substring to the End of a String ................................................................................................. 135(cid:1) Appending a Substring ........................................................................................................................................ 136(cid:1) Inserting a String ................................................................................................................................................ 137(cid:1) Summary ............................................................................................................................................................ 138(cid:1) ■Chapter 10: Arrays ...................................................................................... 139(cid:1) Creating an Array ................................................................................................................................................ 140(cid:1) Finding the Right Method to Use .................................................................................................................... 141(cid:1) Storing Objects in an Array ............................................................................................................................ 143(cid:1) Additional Methods for Filling an Array .......................................................................................................... 145(cid:1) Counting the Items Stored in an Array ................................................................................................................ 145(cid:1) Accessing an Item in an Array ............................................................................................................................ 146(cid:1) Accessing All Items in an Array .......................................................................................................................... 147(cid:1) vii www.it-ebooks.info ■ CONTENTS Adding Items to an Array .................................................................................................................................... 149(cid:1) Inserting Items into an Array .............................................................................................................................. 151(cid:1) Deleting Items from an Array .............................................................................................................................. 152(cid:1) Deleting the Last Item in an Array ................................................................................................................. 152(cid:1) Deleting an Item from a Specific Index Position ............................................................................................ 152(cid:1) Deleting Every Item from an Array ................................................................................................................. 153(cid:1) Deleting All Instances of an Item from an Array ............................................................................................ 153(cid:1) Summary ............................................................................................................................................................ 155(cid:1) ■Chapter 11: Dictionaries and Sets ............................................................... 157(cid:1) Dictionary Basics ................................................................................................................................................ 157(cid:1) Creating and Putting Data in a Dictionary ........................................................................................................... 158(cid:1) Counting the Items Stored in a Dictionary .......................................................................................................... 159(cid:1) Retrieving an Item from a Dictionary .................................................................................................................. 160(cid:1) Deleting Data from a Dictionary .......................................................................................................................... 161(cid:1) Copying a Dictionary ........................................................................................................................................... 162(cid:1) Copying Dictionary Data Into an Array ................................................................................................................ 163(cid:1) Sorting Keys ........................................................................................................................................................ 164(cid:1) Access All Items in a Dictionary .......................................................................................................................... 165(cid:1) Using Sets ........................................................................................................................................................... 166(cid:1) Creating and Putting Data in a Set ...................................................................................................................... 166(cid:1) Counting the Number of Items in a Set ............................................................................................................... 167(cid:1) Checking Whether Data Is in a Set ..................................................................................................................... 167(cid:1) Adding and Removing Data in a Set ................................................................................................................... 168(cid:1) Accessing All Items in a Set ................................................................................................................................ 169(cid:1) Getting the Intersection of Two Sets ................................................................................................................... 170(cid:1) Identifying a Subset of a Set ............................................................................................................................... 170(cid:1) Summary ............................................................................................................................................................ 172(cid:1) ■Chapter 12: Creating Classes and Objects .................................................. 173(cid:1) Creating a Class .................................................................................................................................................. 174(cid:1) Understanding the Code in a Class ..................................................................................................................... 176(cid:1) Deleting Class Files ............................................................................................................................................. 177(cid:1) A Program Example of a Class ............................................................................................................................ 178(cid:1) Creating Methods ................................................................................................................................................ 180(cid:1) Passing Parameters ....................................................................................................................................... 183(cid:1) Returning Values from a Method ................................................................................................................... 188(cid:1) Passing by Reference .................................................................................................................................... 192(cid:1) Creating Class Properties ................................................................................................................................... 195(cid:1) Defining Properties ........................................................................................................................................ 196(cid:1) Accessing and Getting Values in Properties .................................................................................................. 197(cid:1) Summary ............................................................................................................................................................ 200(cid:1) ■Chapter 13: Inheritance, Method Overriding, and Events ............................ 203(cid:1) Object Inheritance ............................................................................................................................................... 203(cid:1) Method Overriding .............................................................................................................................................. 207(cid:1) Responding to Events ......................................................................................................................................... 209(cid:1) Understanding the Application Delegate ........................................................................................................ 210(cid:1) Summary ............................................................................................................................................................ 214(cid:1) viii www.it-ebooks.info ■ CONTENTS ■Chapter 14: Creating a User Interface ......................................................... 215(cid:1) Getting to Know Interface Builder ....................................................................................................................... 215(cid:1) Creating a New User Interface .xib File ......................................................................................................... 216(cid:1) Understanding the Parts of a .XIB File ................................................................................................................ 218(cid:1) Placeholder Objects ....................................................................................................................................... 219(cid:1) Interface Objects ............................................................................................................................................ 219(cid:1) Toggling the View of Placeholder and Interface Objects ............................................................................... 220(cid:1) Designing a User Interface .................................................................................................................................. 221(cid:1) Customizing User Interface Objects .................................................................................................................... 226(cid:1) Moving and Resizing User Interface Objects ................................................................................................. 226(cid:1) Autosizing and Anchoring User Interface Objects .......................................................................................... 227(cid:1) Summary ............................................................................................................................................................ 229(cid:1) ■Chapter 15: Choosing Commands with Buttons .......................................... 231(cid:1) Creating a Button ................................................................................................................................................ 232(cid:1) Creating a Button Title ........................................................................................................................................ 235(cid:1) Adding a Graphic Image ...................................................................................................................................... 237(cid:1) Customizing the Visual Behavior of a Button ...................................................................................................... 239(cid:1) Making Buttons Easier to Use ............................................................................................................................. 240(cid:1) Creating Tooltips ............................................................................................................................................ 240(cid:1) Adding Sound ................................................................................................................................................. 241(cid:1) Choosing a Button with a Keystroke Combination ......................................................................................... 241(cid:1) Connecting a Button to an IBAction .................................................................................................................... 242(cid:1) Alternate Dragging Option ............................................................................................................................. 245(cid:1) Breaking a Link to an IBAction Method ............................................................................................................... 246(cid:1) Summary ............................................................................................................................................................ 248(cid:1) ■Chapter 16: Making Choices with Radio Buttons and Check Boxes ............ 249(cid:1) Radio Buttons ...................................................................................................................................................... 250(cid:1) Creating and Adding Radio Buttons ............................................................................................................... 250(cid:1) Creating a Radio Button Title ......................................................................................................................... 253(cid:1) Defining a Radio Button’s State ..................................................................................................................... 253(cid:1) Determining Which Radio Button a User Selected .............................................................................................. 253(cid:1) Check Boxes ....................................................................................................................................................... 257(cid:1) Creating Check Boxes .................................................................................................................................... 257(cid:1) Defining a Check Box’s Title and State .......................................................................................................... 259(cid:1) Summary ............................................................................................................................................................ 262(cid:1) ■Chapter 17: Making Choices with Pop-Up Buttons ...................................... 263(cid:1) Pop-Up Button Basics ......................................................................................................................................... 263(cid:1) Creating a Pop-Up Button List in Interface Builder ............................................................................................. 266(cid:1) Adding (and Deleting) Items on a Pop-Up Button List .................................................................................... 267(cid:1) Renaming an Item in a Pop-Up Button List .................................................................................................... 270(cid:1) Modifying a Pop-Up Button’s List with Code ....................................................................................................... 270(cid:1) Determining What a User Selected ..................................................................................................................... 274(cid:1) Summary ............................................................................................................................................................ 277(cid:1) ■Chapter 18: Inputting and Outputting Data with Labels, Text Fields, and Combo Boxes ................................................. 279(cid:1) Using Labels ....................................................................................................................................................... 279(cid:1) ix www.it-ebooks.info

Description:
Want to learn how to program on your Mac? Not sure where to begin? Best-selling author Wallace Wang will explain how to get started with Cocoa, Objective-C, and Xcode. Whether you are an experienced Windows coder moving to the Mac, or you are completely new to programming, you'll see how the basic d
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.