mac programming for absolute beginners

415 998 0
mac  programming  for  absolute  beginners

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® W ant 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 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 for Absolute Beginners Wallace Wang Get started with Objective-C,Cocoa, and Xcode on the Mac www.it-ebooks.info i Mac Programming for Absolute Beginners ■ ■ ■ Wallace Wang www.it-ebooks.info ii 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 orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, 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. www.it-ebooks.info iii This book is dedicated to all those dreamers who just needed a helping hand to turn their great ideas into a working program. www.it-ebooks.info iv Contents at a Glance ■Contents v ■About the Author xii ■About the Technical Reviewer xiii ■Acknowledgments xiv ■Introduction xv ■Chapter 1: Understanding Programming 1 ■Chapter 2: Understanding Apple’s Programming Tools 17 ■Chapter 3: The Basic Steps to Creating a Mac Program 29 ■Chapter 4: Getting Help 47 ■Chapter 5: Learning Objective-C 63 ■Chapter 6: Making Decisions with Branches 83 ■Chapter 7: Repeating Code with Loops 99 ■Chapter 8: Understanding the Cocoa Framework 111 ■Chapter 9: Manipulating Strings 123 ■Chapter 10: Arrays 139 ■Chapter 11: Dictionaries and Sets 157 ■Chapter 12: Creating Classes and Objects 173 ■Chapter 13: Inheritance, Method Overriding, and Events 203 ■Chapter 14: Creating a User Interface 215 ■Chapter 15: Choosing Commands with Buttons 231 ■Chapter 16: Making Choices with Radio Buttons and Check Boxes 249 ■Chapter 17: Making Choices with Pop-Up Buttons 263 ■Chapter 18: Inputting and Outputting Data with Labels, Text Fields, and Combo Boxes 279 ■Chapter 19: Inputting Data with Sliders, Date Pickers, and Steppers 299 ■Chapter 20: Using Built-In Dialog Boxes 315 ■Chapter 21: Creating Pull-Down Menus 331 ■Chapter 22: Designing Your Own Programs 343 ■Chapter 23: Working with Xcode 361 ■Chapter 24: Debugging Your Program 371 ■Index 385 www.it-ebooks.info v Contents ■Contents at a Glance iv ■About the Author xii ■About the Technical Reviewer xiii ■Acknowledgments xiv ■Introduction xv ■Chapter 1: Understanding Programming 1 Programming Principles 2 Dividing Programs into Parts 5 Event-Driven Programming 7 Object-Oriented Programming 8 Understanding Programming Languages 11 The Building Blocks of Programming Languages 12 Programming Frameworks 13 Mac Programming Today 14 Summary 16 ■Chapter 2: Understanding Apple’s Programming Tools 17 Understanding Editors 17 Understanding Xcode 18 Deciphering the Xcode User Interface 19 Running Xcode 20 Creating a New Project in Xcode 21 Examining Project Files in Xcode 24 Compiling a Program 26 Summary 27 ■Chapter 3: The Basic Steps to Creating a Mac Program 29 A Bare-Bones Program Example 30 A Simple User Interface Example 33 An Interactive User Interface Example 37 Writing Objective-C Code 38 Connecting the User Interface 40 www.it-ebooks.info ■ CONTENTS vi An Advanced Interactive User Interface Example 42 Summary 45 ■Chapter 4: Getting Help 47 Installing Help Topics 47 Getting Help About Xcode 48 Getting Help About Core Library 49 Searching for Help 54 Getting Quick Help 55 Viewing Documentation for Selected Text 56 Getting Help with Library Windows 57 Help While Writing Code 59 Color-Coding 59 Customizing the Editor 60 Using Code Completion 60 Summary 61 ■Chapter 5: Learning Objective-C 63 Differences in Writing a Mac Objective-C Program 63 Understanding Objective-C Symbols 65 Defining the End of Each Line with a Semicolon 66 Defining the Beginning and End of Code with Curly Brackets 67 Defining Compiler Directives with the # Symbol 68 Defining Comments with // 68 Identifying Objects with [ and ] 69 Defining Pointers with * 70 Manipulating Data with Variables 71 Declaring Variables 71 Assigning Data to a Variable 73 The Scope of a Variable 73 A Program Example Using Variables 75 Using Constants 76 Using Mathematical Operators 78 Using Strings 79 Summary 81 ■Chapter 6: Making Decisions with Branches 83 Understanding Boolean Expressions 84 Boolean Comparison Operators 86 Boolean Logical Operators 87 Branches 90 The Simplest if Statement 90 Following Multiple Instructions in an if Statement 91 The if-else Statement 92 The if-else if Statement 92 The switch Statement 94 Summary 98 ■Chapter 7: Repeating Code with Loops 99 Loops That Run a Fixed Number of Times 100 Quitting a for Loop Prematurely 102 www.it-ebooks.info ■ CONTENTS vii Skipping in a for Loop 103 Loops That Run Zero or More Times 104 The while Loop 104 The do-while Loop 105 Quitting a while or do-while Loop Prematurely 106 Skipping a while or do-while Loop 107 Nested Loops 107 Summary 109 ■Chapter 8: Understanding the Cocoa Framework 111 An Overview of How Object-Oriented Programming Works 112 Starting with a Class 113 Reducing Bugs 114 Reusing Code 114 Defining Classes 114 Creating an Object 115 Storing Data in an Object 116 A Sample Program for Manipulating Objects 117 Looking Up Method and Property Names for NS Classes 118 Summary 121 ■Chapter 9: Manipulating Strings 123 Declaring a String Variable 123 Getting the Length of a String 124 Comparing Two Strings 125 Checking for Prefixes and Suffixes 125 Converting to Uppercase and Lowercase 126 Converting Strings to Numbers 127 Searching for a Substring 129 The location Field 129 The length Field 129 Searching and Replacing 130 Replacing Part of a String at a Specific Location 130 Searching for and Replacing Part of a String 132 Deleting Part of a String 133 Extracting a Substring 134 Extracting a Substring with a Location and Length 134 Extracting a Substring to the End of a String 135 Appending a Substring 136 Inserting a String 137 Summary 138 ■Chapter 10: Arrays 139 Creating an Array 140 Finding the Right Method to Use 141 Storing Objects in an Array 143 Additional Methods for Filling an Array 145 Counting the Items Stored in an Array 145 Accessing an Item in an Array 146 Accessing All Items in an Array 147 www.it-ebooks.info ■ CONTENTS viii Adding Items to an Array 149 Inserting Items into an Array 151 Deleting Items from an Array 152 Deleting the Last Item in an Array 152 Deleting an Item from a Specific Index Position 152 Deleting Every Item from an Array 153 Deleting All Instances of an Item from an Array 153 Summary 155 ■Chapter 11: Dictionaries and Sets 157 Dictionary Basics 157 Creating and Putting Data in a Dictionary 158 Counting the Items Stored in a Dictionary 159 Retrieving an Item from a Dictionary 160 Deleting Data from a Dictionary 161 Copying a Dictionary 162 Copying Dictionary Data Into an Array 163 Sorting Keys 164 Access All Items in a Dictionary 165 Using Sets 166 Creating and Putting Data in a Set 166 Counting the Number of Items in a Set 167 Checking Whether Data Is in a Set 167 Adding and Removing Data in a Set 168 Accessing All Items in a Set 169 Getting the Intersection of Two Sets 170 Identifying a Subset of a Set 170 Summary 172 ■Chapter 12: Creating Classes and Objects 173 Creating a Class 174 Understanding the Code in a Class 176 Deleting Class Files 177 A Program Example of a Class 178 Creating Methods 180 Passing Parameters 183 Returning Values from a Method 188 Passing by Reference 192 Creating Class Properties 195 Defining Properties 196 Accessing and Getting Values in Properties 197 Summary 200 ■Chapter 13: Inheritance, Method Overriding, and Events 203 Object Inheritance 203  Method Overriding 207 Responding to Events 209 Understanding the Application Delegate 210  Summary 214 www.it-ebooks.info ■ CONTENTS ix ■Chapter 14: Creating a User Interface 215 Getting to Know Interface Builder 215 Creating a New User Interface .xib File 216 Understanding the Parts of a .XIB File 218 Placeholder Objects 219  Interface Objects 219 Toggling the View of Placeholder and Interface Objects 220 Designing a User Interface 221 Customizing User Interface Objects 226 Moving and Resizing User Interface Objects 226 Autosizing and Anchoring User Interface Objects 227 Summary 229 ■Chapter 15: Choosing Commands with Buttons 231 Creating a Button 232 Creating a Button Title 235 Adding a Graphic Image 237 Customizing the Visual Behavior of a Button 239 Making Buttons Easier to Use 240 Creating Tooltips 240 Adding Sound 241 Choosing a Button with a Keystroke Combination 241 Connecting a Button to an IBAction 242 Alternate Dragging Option 245  Breaking a Link to an IBAction Method 246  Summary 248 ■Chapter 16: Making Choices with Radio Buttons and Check Boxes 249 Radio Buttons 250 Creating and Adding Radio Buttons 250 Creating a Radio Button Title 253 Defining a Radio Button’s State 253 Determining Which Radio Button a User Selected 253 Check Boxes 257 Creating Check Boxes 257 Defining a Check Box’s Title and State 259 Summary 262 ■Chapter 17: Making Choices with Pop-Up Buttons 263 Pop-Up Button Basics 263 Creating a Pop-Up Button List in Interface Builder 266 Adding (and Deleting) Items on a Pop-Up Button List 267 Renaming an Item in a Pop-Up Button List 270 Modifying a Pop-Up Button’s List with Code 270 Determining What a User Selected 274 Summary 277 ■Chapter 18: Inputting and Outputting Data with Labels, Text Fields, and Combo Boxes 279 Using Labels 279 www.it-ebooks.info [...]... nothing about programming at all, this book is for you No matter what your skill level may be, you can learn how to write programs for the Macintosh starting today What makes programming for the Macintosh so appealing is that the programming tools are free (courtesy of Apple), and by learning the basics of programming the Macintosh, you can easily apply your skills and experience to programming the... Author Wallace Wang is a former Windows enthusiast who took one look at Vista and realized that the future of computing belonged to the Macintosh He’s written more than 40 computer books including Microsoft Office for Dummies, Beginning Programming for Dummies, Steal This Computer Book, My New Mac, and My New iPad In addition to programming the Macintosh and iPhone/iPad, he also performs stand-up comedy,... programmed before in your life or if you’re already familiar with programming but not with Macintosh programming, then this book is for you Even if you’re experienced with Macintosh programming, you may still find this book handy as a reference to help you achieve certain results without having to wade through several books to find an answer To help you learn the different principles behind Macintosh programming, ... copies Understanding Programming Languages Once you understand how programming has gradually evolved and how Mac programming requires understanding all of these different programming techniques, you’re almost ready to start programming Next, you need to know that giving instructions to a computer involves writing commands using a programming language 11 12 CHAPTER 1: Understanding Programming There are... learn how to write programs for the Mac, you need to learn three separate skills First, you need to understand the basic principles of programming That includes organizing instructions in sequences, loops, or branches Second, you need to learn a specific programming language For the Mac, you’ll be learning Objective-C Objective-C is designed more for program efficiency and less for human readability, which... sell your programming skills to create custom software for others, you’ll find that programming is a skill anyone can learn Programming is nothing more than problem solving using a particular programming language By knowing how to solve problems using Objective-C, you can create your own programs for the Mac much easier and far faster than you might think 17 Chapter 2 Understanding Apple’s Programming. .. technical information needed to create super-sophisticated programs that involve graphics or sound If you just want to get started and learn the basics of programming using Apple’s programming tools, this book is for you If you’re already an experienced Windows programmer and want to get started programming the Macintosh, this book can be especially helpful in teaching you the basics of using Apple’s programming. .. program’s user interface View: Commands for hiding or displaying icons or windows that display additional information Product: Commands for compiling and testing your program Build: Commands for compiling your programs Run: Commands for testing and debugging your program Window: Commands for manipulating Xcode’s windows that may display additional commands Help: Commands for getting help using Xcode or writing... contain subprograms for calculating mathematical formulas By using commands to create subprograms, programmers can create an endless variety of additional building blocks for making any type of program To make programming the Mac, iPhone, and iPad easier, Apple has created libraries or frameworks of useful subprograms that you can use in your own programs There are two reasons for reusing an existing... the instructions for only a single subprogram, not for a dozen or more other subprograms To write any program, most programmers divide a large program into subprograms and store those subprograms in separate files CHAPTER 1: Understanding Programming Event-Driven Programming In the early days of computers, most programs forced people to use a program by typing one command at a time For example, if . Objective-C,Cocoa, and Xcode on the Mac www.it-ebooks.info i Mac Programming for Absolute Beginners ■ ■ ■ Wallace Wang www.it-ebooks.info ii Mac Programming for Absolute Beginners Copyright. 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 for Absolute Beginners Wallace Wang Get started with. Microsoft Office for Dummies, Beginning Programming for Dummies, Steal This Computer Book, My New Mac, and My New iPad. In addition to programming the Macintosh and iPhone/iPad, he also performs stand-up

Ngày đăng: 05/05/2014, 14:59

Từ khóa liên quan

Mục lục

  • Contents at a Glance

  • Contents

  • About the Author

  • About the TechnicalReviewer

  • Acknowledgments

  • Introduction

    • Code Conventions Used in This Book

    • What to Expect from This Book

    • Understanding Programming

      • Programming Principles

        • Dividing Programs into Parts

        • Event-Driven Programming

        • Object-Oriented Programming

        • Understanding Programming Languages

          • The Building Blocks of Programming Languages

          • Programming

          • Frameworks

          • Mac Programming Today

          • Summary

          • Understanding Apple’s Programming Tools

            • Understanding Editors

            • Understanding Xcode

              • Deciphering the Xcode User Interface

              • Running Xcode

              • Creating a New Project in Xcode

              • Examining Project Files in Xcode

Tài liệu cùng người dùng

Tài liệu liên quan