首页 > > > [iPhone和iPad开发书籍大全].Objective.C.Visual.QuickSta…

[iPhone和iPad开发书籍大全].Objective.C.Visual.QuickStart.Guide(Peachpit.Press.2010-02).pdf

[iPhone和iPad开发书籍大全].Objective.C…

上传者: lkgroidwdfwg 2011-03-18 评分1 评论0 下载43 收藏10 阅读量433 暂无简介 简介 举报

简介:本文档为《[iPhone和iPad开发书籍大全].Objective.C.Visual.QuickStart.Guide(Peachpit.Press.2010-02)pdf》,可适用于手机软件领域,主题内容包含ptgFromtheLibraryofWow!eBookptgVISUALQUICKStArtGUIDEobjectivecStevenHolzne符等。

ptg From the Library of Wow! eBook ptg VISUAL QUICKStArt GUIDE objective-c Steven Holzner Peachpit Press From the Library of Wow! eBook ptg Visual QuickStart Guide Objective-C Steven Holzner Peachpit Press 1249 Eighth Street Berkeley, CA 94710 510/524-2178 510/524-2221 ( fax) Find us on the Web at www.peachpit.com. To report errors, please send a note to errata@peachpit.com. Peachpit Press is a division of Pearson Education. Copyright 2010 by Steven Holzner Editor: Judy Ziajka Production Coordinator: Myrna Vladic Compositor: Deb Roberti Proofreader: Wendy Sharp Indexer: FireCrystal Communications Cover Design: Peachpit Press Notice of rights All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact permissions@peachpit.com. Notice of Liability The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press 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 instructions contained in this book or by the computer software and hardware products described in it. trademarks Visual QuickStart Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Any other product names used in this book may be trademarks of their own respective owners. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book. ISBN 13: 978-0-321-69946-6 ISBN 10: 0-321-69946-7 9 8 7 6 5 4 3 2 1 Printed and bound in the United States of America From the Library of Wow! eBook ptg Dedication To Nancy, of course! Acknowledgments The book you hold in your hands is the product of many people’s work. I would particularly like to thank Wendy Sharp and Judy Ziajka for their tireless efforts to make this book the best it can be and Danny Kalev for his careful technical review of the entire manuscript. From the Library of Wow! eBook ptg iv Ta bl e of C on te nt s Introduction viii Chapter 1: Getting Started: Essential Objective-C 1 Creating Your First Program. . . . . . . . . . . . . . . . . . . . . . 3 Compiling and Running Your First Program . ......... 6 Using Variables . .................................................................... 8 Displaying Values in Variables . ....................................... 9 Working with Data Types . ............................................. 11 Adding Comments . ............................................................ 13 Using Arithmetic Operators . ......................................... 15 Using Assignment Operators . ....................................... 17 Using the Increment and Decrement Operators . ........................................... 19 Changing Type with Cast Operators . ...................... 21 Chapter 2: Directing Program Flow 23 Using the if Statement . ................................................... 26 Using the else Statement . ............................................... 27 Using the switch Statement . ......................................... 29 Using Comparison Operators . ..................................... 31 Using Logical Operators . ............................................... 32 Using the Conditional Operator . ................................ 33 Using the for Loop . ............................................................ 35 Using the while Loop . ...................................................... 37 Using the do...while Loop . ............................................. 39 Using the break Statement . ......................................... 41 Chapter 3: Handling Data 43 About Creating NS-Class Objects . .............................. 45 Creating Arrays . .................................................................. 46 Initializing Arrays . .............................................................. 47 Looping over Arrays . ........................................................ 48 Creating Two-Dimensional Arrays . .......................... 49 Using Pointers . .................................................................... 51 Using Pointer Math . .......................................................... 52 Interchanging Pointers and Arrays . .......................... 53 Using Strings . ....................................................................... 54 Passing Messages to String Objects . .......................... 56 Using Enumerations . ........................................................ 57 Table of Contents From the Library of Wow! eBook ptg v Table of Contents Table of Contents Chapter 4: Creating Functions 59 Defining a Function . .......................................................... 61 Declaring Functions Using Prototypes . .................... 62 Passing Arguments to Functions . .............................. 64 Returning Values from Functions . .............................. 66 Using Function Scope . ...................................................... 68 Passing Pointers to Functions . ..................................... 70 Passing Arrays to Functions . ....................................... 72 Passing Constant Data to Functions . ........................ 74 Using Recursion . ................................................................ 76 Using Pointers to Functions . ....................................... 77 Chapter 5: Classes and Objects 79 Creating Objective-C Classes and Objects . ........... 82 Using Class Methods . ........................................................ 84 Creating an Object . ............................................................ 86 Creating Object Methods . ............................................. 87 Storing Data in Objects . ................................................. 88 Passing Multiple Arguments to Methods . ............. 90 Storing the Interface in a Header File . ...................... 92 Adding the Implementation to the Header File . . . 94 Linking Multiple Files . ................................................... 95 Using Constructors . .......................................................... 97 Chapter 6: Object-Oriented Programming 99 About Access Specifiers . ............................................... 100 Using Public Access . ...................................................... 102 Using Private Access . ................................................... 103 Using Protected Access . ............................................... 105 Using Class Variables . ................................................... 107 Accessing the Current Object . .................................. 109 Creating a Variable for Multiple Object Types . ............................................................ 111 Verifying That an Object Belongs to a Class .... 113 Checking an Object's Class with isKindOfClass . .......................................................... 115 Verifying That an Object Supports a Method .... 117 Checking Whether Objects Support a Method ... 118 Chapter 7: Working with Object-Oriented Inheritance 119 Inheriting from a Class . ............................................... 121 Inheriting Base-Class Data Members . ................. 122 Inheriting Base-Class Methods . .............................. 124 Overriding Base-Class Methods . .............................. 126 Overloading Base-Class Methods . .......................... 128 From the Library of Wow! eBook ptg vi Table of Contents Ta bl e of C on te nt s Using Multi-level Inheritance . .................................. 130 Limiting Access . .............................................................. 132 Restricting Access . .......................................................... 134 Using Constructors with Inheritance . ................. 136 Using Polymorphism . ................................................... 138 Chapter 8: Categories, Posing, and Protocols 141 About Categories . ............................................................ 143 Categories: Creating the Base Class . ...................... 145 Categories: Creating Categories . .............................. 146 Categories: Putting It All Together . .......................... 147 About Posing . .................................................................... 149 Posing: Creating the Base Class . ................................ 151 Posing: Creating the Derived Class . ........................ 152 Posing: Putting It All Together . .................................. 153 About Protocols . .............................................................. 155 Protocols: Defining the Protocol and Interfaces . ................................................................ 157 Protocols: Creating the Class Implementations . ................................................... 159 Protocols: Putting It All Together . ............................ 161 Chapter 9: Using Arrays and Dictionaries 163 Creating an Array . .......................................................... 165 Accessing Array Elements . ........................................... 166 Using Enumeration to Loop over an Array . ......... 167 Creating a Mutable Array . ........................................... 169 Adding Elements to a Mutable Array . .................... 171 Sorting an Array . .............................................................. 173 Releasing Array Memory . ............................................. 175 Creating a Dictionary . ................................................... 176 Enumerating a Dictionary . ....................................... 178 Creating a Mutable Dictionary . ................................ 180 Adding Objects to a Mutable Dictionary . ............. 181 Chapter 10: Managing Memory in Objective-C 183 Creating Test Objects . ................................................... 185 Displaying the Retain Count . ..................................... 186 Incrementing an Object’s Retain Count . ............... 188 Decrementing an Object’s Retain Count . ........... 190 Deallocating Objects from Memory . ...................... 192 Using an Autorelease Pool . ......................................... 194 Using Self-Managed Memory . .................................. 195 Deallocating Memory Yourself: Creating the Class . ..................................................................... 197 From the Library of Wow! eBook ptg vii Table of Contents Table of Contents vii Deallocating Memory Yourself: Storing Internal Objects . ...................................................... 198 Deallocating Memory Yourself: Creating the main Method . ................................................... 200 Deallocating Memory Yourself: Performing Deallocation . ............................................................ 201 Chapter 11: Exception Handling 203 Catching Exceptions . ...................................................... 205 Handling Exceptions . ...................................................... 206 Using the End Handler . ................................................. 207 Creating an Exception . ................................................. 209 Checking What Exception Occurred . .................... 211 Handling Multiple Exceptions . .................................. 213 Passing Exceptions Up the Call Stack . .................... 215 Returning Values from Exception Handlers . ....... 217 Returning void from an Exception Handler . ....... 219 Catching Uncaught Exceptions . .............................. 221 Index 223 From the Library of Wow! eBook ptg viii In tr od uc ti on i Welcome to Objective-C. This book is your guided tour of this exciting language, and it gives you what you need to start working with Objective-C at once. Using Objective-C, you can write professional programs that make use of many object- oriented features—from the basics up to advanced class inheritance and exception (run-time error) handling. Objective-C runs on many different platforms. For the most part, your code should work unchanged on all platforms that Objective-C supports, but where differences in support exist, this book points them out to you. This book starts with the basics and contin- ues on through advanced topics. You’ll begin by looking at how to get Objective-C started and how to run basic programs. From there, you’ll explore data handling, again start- ing with the basics and moving on through advanced topics. After looking at how to write your own functions, you’ll wrap functions and data together into objects—the core of Objective-C programming. And when you start working with object-oriented programming, the lid is off—and we’ll push the envelope as far as it can go. Introduction That’s the plan, then: to present a guided tour of Objective-C, taking you from the beginning to the most advanced topics. Let’s get started with Chapter 1 now. From the Library of Wow! eBook ptg 1 G etting Started: Essential O bjective-C 1 This book takes you on a guided tour of Objective-C, from the basics on up through the cool stuff. Objective-C is a cross-platform language, so you’ll find it on many systems: the Mac, of course, but also Linux, UNIX, Windows, and more—and its core programming code stays the same across all those platforms. Objective-C is actually a layer built on top of the C language, and everything that works in standard (that is, ANSI) C works in Objective-C. Objective-C also adds tons of object-oriented features to the original C language. The way it uses objects is what makes Objective-C so popular, but just what is an object? Object-oriented programming was introduced when programs began to get very large and the structure of the code began to get in the way. Object-oriented programming lets programmers wrap whole sections of their code into easily handled, self-contained objects and so let them break up their code. continues on next page Getting Started: Essential Objective-C From the Library of Wow! eBook ptg Chapter 1 2 G et ti ng S ta rt ed : E ss en ti al O bj ec ti ve -C For example, say you have a bowl of pudding that you want to keep cold. You could set up a system of coolant pipes, switches, and dials that cool your pudding but which take your constant attention: you have to watch the temperature, and when the pudding gets too warm, you have to turn on the coolant compressor and pump and so on; when the pudding gets cold enough, you have switch those things off. That was the old way of programming, with the guts of every item in your program laid bare to the whole rest of the program. Object-oriented programming, by contrast, lets you wrap all that functionality into a sin- gle object: a refrigerator. The refrigerator’s job is to keep things like pudding cold without a lot of fuss on your part. It is responsible for maintaining its own internal state—that is, remaining cold inside. It has thermostats and relays and the like to automatically handle the jobs you previously did manually. So if you want your pudding kept cold, simply put it in the refrigerator. All the details are hidden from view, and your kitchen becomes a much easier place to handle conceptually. So it is with object-oriented programming. Now you can wrap code and data together into objects that are self-contained, and because all the details are hidden, your interaction with those objects becomes a lot simpler. That’s the secret behind object-oriented programming: divide and conquer. In this book, you’ll see what makes the objects in Objective-C tick. They’re different than the objects in other languages—they communicate with messages; you don’t call the code in them directly—but they’re just as powerful, and often more so. We’ll start in this chapter with the basics: handling basic data items, printing results from Objective-C programs, running your programs, and more. From the Library of Wow! eBook ptg Getting Started: Essential Objective-C 3 Creating Your First Program Creating Your First Program We’re going to jump right into Objective-C by creating and running a program, which we’ll name first.m. We’ll start by creating a function—that is, a bit of code that you can call by name— named main( ): int main(void) {         .         .         . } Functions can be passed data, as you’ll see later, but this function isn’t passed any data, which is why we use the keyword void in the parentheses. Functions can also return values, and the main( ) function returns an integer value to Objective-C indicating whether the program succeeded. The int in front of main( ) tells Objective-C to expect an integer return value. The code for the main( ) function goes inside curly braces: { and }. Next, we’ll use the built-in Objective-C func- tion named printf( ) to display some text. We pass the text we want printf( ) to display inside parentheses: int main(void) { printf (“Welcome to Objective-C!”);         .         .         . } continues on next page tip The extension for Objective-C code files is .m. From the Library of Wow! eBook ptg Chapter 1 4 Cr ea ti ng Y ou r Fi rs t Pr og ra m To use the printf( ) function, we have to tell Objective-C about that function with a function declaration, as you’ll see when we discuss how to create functions. The decla- rations for the standard I/O functions like printf( ) are contained in an Objective-C file named stdio.h, where .h stands for “header file”; we include stdio.h in our program as shown here so Objective-C knows about the printf( ) function: #include <stdio.h> int main(void) {   printf (“Welcome to Objective-C!”);         .         .         . } When the program ends, Objective-C will expect some indication of whether the func- tion succeeded. We’ll return a value of 0 to Objective-C, which means there were no errors. Listing 1.1 shows the entire program, which you will create step by step in the following tasks. tip Note that #import and #include are the same for our purposes. You can use them interchangeably and in any order. #include <stdio.h> int main(void) {   printf (“Welcome to Objective-C!”); return 0; } Listing 1.1 Your first Objective-C program. From the Library of Wow! eBook ptg Getting Started: Essential Objective-C 5 Creating Your First Program to create your first Objective-C program on the Mac: 1. From http://developer.apple.com/ iphone, download and install the Xcode Integrated Development Environment. 2. Run Xcode. 3. Choose File > New Project. 4. In the New Project window, choose Application. 5. Click the Command-Line Tool icon to select it. 6. From the Type drop-down menu, choose Foundation. 7. Click the Choose button. 8. Enter First as the name of your application. 9. Select a save locatio

该用户的其他资料

  • 名称/格式
  • 评分
  • 下载次数
  • 资料大小
  • 上传时间
  • 0
    1179
    28.5MB
    2011-03-18

    数据挖掘概念与技术 英文原版 第二版  本书全面地讲述数据挖掘领域的重要知识和技术创新。在第1版内容相当全面的基础上,第2版展示了该领域的最新研究成果,例如挖掘流、时序和序列数据以及挖掘时间空间、多媒体、文本和Web…[立即查看]

  • 0
    1281
    353.1KB
    2011-03-18
  • 0
    531
    13.1MB
    2011-03-18
  • 0
    595
    4.6MB
    2011-03-18
  • 0
    371
    24.7MB
    2011-03-18

    国内程序员写的开源机器学习算法库NPatternRecognizer 部分内容就是参照的这本书。 NPatternRecognizerhj.................................[立即查看]

  • 国内程序员写的开源机器学习算法库NPatternRecognizer 部分内容就是参照的这本书。 NPatternRecognizerhj.................................[立即查看]

  • 0
    95
    34.0MB
    2011-03-18

    国内程序员写的开源机器学习算法库NPatternRecognizer 部分内容就是参照的这本书。 NPatternRecognizerhj.................................[立即查看]

  • 0
    87
    31.0MB
    2011-03-18

    国内程序员写的开源机器学习算法库NPatternRecognizer 部分内容就是参照的这本书。 NPatternRecognizerhj.................................[立即查看]

  • 0
    145
    27.5MB
    2011-03-18

    国内程序员写的开源机器学习算法库NPatternRecognizer 部分内容就是参照的这本书。 NPatternRecognizerhj.................................[立即查看]

  • 0
    579
    36.3MB
    2011-03-18

    国内程序员写的开源机器学习算法库NPatternRecognizer 部分内容就是参照的这本书。 NPatternRecognizer:http://npatternrecognizer.codeplex.com[立即查看]

用户评论

0/200
    暂无评论
上传我的资料

相关资料

资料评价:

/ 240
所需积分:1 立即下载
返回
顶部
举报
资料
关闭

温馨提示

感谢您对爱问共享资料的支持,精彩活动将尽快为您呈现,敬请期待!