文件名称:Programming in Objective-C 4th Edition
文件大小:5.92MB
文件格式:PDF
更新时间:2017-01-05 15:16:49
Programming in Objective-C 4th Edition
目录
需要英语好一点的人看
Contents
1 Introduction 1
What You Will Learn from This Book
2
7
19 23
27
How This Book Is Organized Support 5 Acknowledgments 5 Preface to the Fourth Edition
2 Programming in Objective-C
3
6
7
Compiling and Running Programs Using Xcode 8
Using Terminal
Explanation of Your First Program Displaying the Values of Variables Summary 25
Exercises 25
3 Classes, Objects, and Methods
17
What Is an Object, Anyway? 27
Instances and Methods 28
An Objective-C Class for Working with Fractions 30 The @interface Section 33
Choosing Names 34
Class and Instance Methods 35
The @implementation Section 37
The program Section 39
Accessing Instance Variables and Data Encapsulation 45 Summary 49
Exercises 49
4 Data Types and Expressions 51
Data Types and Constants 51 Type int 51
Type float 52
Type char 52viii Contents
Qualifiers: long, long long, short, unsigned, and signed 53
Type id 54
Arithmetic Expressions 55
Operator Precedence 55
Integer Arithmetic and the Unary Minus Operator 58 The Modulus Operator 60
Integer and Floating-Point Conversions 61
The Type Cast Operator 63
Assignment Operators 64 A Calculator Class 65 Exercises 67
5 Program Looping 71
The for Statement 72 Keyboard Input 79 Nested for Loops 81 for Loop Variants 83
The while Statement 84 The do Statement 88
The break Statement 90 The continue Statement 90 Summary 91
Exercises 91
6 Making Decisions 93
The if Statement 93
The if-else Construct 98 Compound Relational Tests 100 Nested if Statements 103
The else if Construct 105
The switch Statement 114 Boolean Variables 117
The Conditional Operator 122 Exercises 124
7 More on Classes 127
Separate Interface and Implementation Files 127 Synthesized Accessor Methods 132
Accessing Properties Using the Dot Operator 134 Multiple Arguments to Methods 135
Methods Without Argument Names 137
Operations on Fractions 137 Local Variables 140
Method Arguments 141
The static Keyword 141
The self Keyword 145
Allocating and Returning Objects from Methods 146
Extending Class Definitions and the Interface File 148 Exercises 148
8 Inheritance 151
It All Begins at the Root 151 Finding the Right Method 155
Extension Through Inheritance: Adding New Methods 156 A Point Class and Object Allocation 160
The @class Directive 161
Classes Owning Their Objects 165
Overriding Methods 169
Which Method Is Selected? 171
Abstract Classes 173 Exercises 174
9 Polymorphism, Dynamic Typing,
and Dynamic Binding 177
Polymorphism: Same Name, Different Class 177 Dynamic Binding and the id Type 180
Compile Time Versus Runtime Checking 182 The id Data Type and Static Typing 183
Argument and Return Types with Dynamic Typing 184 Asking Questions About Classes 185
Exception Handling Using @try 189
Exercises 192
Contents ix
x Contents
10 More on Variables and Data Types 195
Initializing Objects 195 Scope Revisited 198
Directives for Controlling Instance Variable Scope 198
More on Properties, Synthesized Accessors, and Instance Variables 200
Global Variables
200 Static Variables 202
Enumerated Data Types 205
208
The typedef Statement Data Type Conversions
209 Conversion Rules 210
Bit Operators 211
The Bitwise AND Operator 212
The Bitwise Inclusive-OR Operator 213 The Bitwise Exclusive-OR Operator 214
The Ones Complement Operator The Left Shift Operator 216 The Right Shift Operator 216
Exercises 217
11 Categories and Protocols 219
Categories 219
Class Extensions 224
214
Some Notes About Categories 225 Protocols and Delegation 226
Delegation 229
Informal Protocols 229 Composite Objects 230 Exercises 231
12 The Preprocessor 233
The #define Statement 233
More Advanced Types of Definitions 235
The #import Statement 240 Conditional Compilation 241
The #ifdef, #endif, #else 241
The #if and #elif Preprocessor Statements 243 The #undef Statement 244
Exercises 245
13 Underlying C Language Features 247
Arrays 248
Initializing Array Elements 250 Character Arrays 251 Multidimensional Arrays 252
Functions 254
Arguments and Local Variables 255 Returning Function Results 257 Functions, Methods, and Arrays 261
Blocks 262 Structures 266
Initializing Structures 269
Structures Within Structures 270
Additional Details About Structures 272
Don’t Forget About Object-Oriented Programming! 273
Pointers 273
Pointers and Structures 277
Pointers, Methods, and Functions 279 Pointers and Arrays 280
Constant Character Strings and Pointers 286 Operations on Pointers 290
Pointers and Memory Addresses 292
They’re Not Objects! 293 Miscellaneous Language Features 293
Compound Literals 293
The goto Statement 294
The null Statement 294
The Comma Operator 294
The sizeof Operator 295 Command-Line Arguments 296
How Things Work 298
Fact #1: Instance Variables Are Stored in Structures 298
Fact #2: An Object Variable Is Really a Pointer 299
Fact #3: Methods Are Functions, and Message Expressions Are Function Calls 299
Fact #4: The id Type Is a Generic Pointer Type 299 Exercises 300
Contents xi
xii Contents
14 Introduction to the Foundation Framework 303
Foundation Documentation 303
15 Numbers, Strings, and Collections 307
Number Objects 307 String Objects 312
More on the NSLog Function 312
The description Method 313
Mutable Versus Immutable Objects 314 Mutable Strings 320
Array Objects 327
Making an Address Book 330 Sorting Arrays 347
Dictionary Objects 354 Enumerating a Dictionary 355
Set Objects 358 NSIndexSet 362
Exercises 365
16 Working with Files 369
Managing Files and Directories: NSFileManager 370 Working with the NSData Class 375
Working with Directories 376
Enumerating the Contents of a Directory 379
Working with Paths: NSPathUtilities.h 381
Common Methods for Working with Paths 383 Copying Files and Using the NSProcessInfo Class 386
Basic File Operations: NSFileHandle 390 The NSURL Class 395
The NSBundle Class 396
Exercises 397
17 Memory Management and Automatic Reference Counting 399
Automatic Garbage Collection 401 Manual Reference Counting 402
Object References and the Autorelease Pool 403
The Event Loop and Memory Allocation 405
Summary of Manual Memory Management Rules 407 Automatic Reference Counting (ARC) 408
Strong Variables 408
Weak Variables 409
@autoreleasepool Blocks 410
Method Names and Non-ARC Compiled Code 411
18 Copying Objects 413
The copy and mutableCopy Methods 413 Shallow Versus Deep Copying 416 Implementing the