Programming in Objective-C, Fifth Edition Updated for OS X Mountain Lion, iOS 6, and Xcode 4.5
Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming for Apple's iOS and OS X platforms.
The book makes no assumptions about prior experience with object-oriented programming languages or with the C language (which Objective-C is based upon). Because of this, both beginners and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C. Readers can also learn the concepts of object-oriented programming without having to first learn all of the intricacies of the underlying C programming language.
This unique approach to learning, combined with many small program examples and exercises at the end of each chapter, makes Programming in Objective-C ideally suited for either classroom use or self-study.
This edition has been fully updated to incorporate new features in Objective-C programming introduced with Xcode 4.4 (OS X Mountain Lion) and Xcode 4.5 (iOS 6.)
“The best book on any programming language that I’ve ever read. If you want to learn Objective-C, buy it.”–Calvin Wolcott
“An excellent resource for a new programmer who wants to learn Objective-C as their first programming language–a woefully underserved market.”–Pat Hughes
Contents at a Glance
1 Introduction
Part I The Objective-C Language
2 Programming in Objective-C
3 Classes, Objects, and Methods
4 Data Types and Expressions
5 Program Looping
6 Making Decisions
7 More on Classes
8 Inheritance
9 Polymorphism, Dynamic Typing, and Dynamic Binding
10 More on Variables and Data Types
11 Categories and Protocols
12 The Preprocessor
13 Underlying C Language Features
Part II The Foundation Framework
14 Introduction to the Foundation Framework
15 Numbers, Strings, and Collections
16 Working with Files
17 Memory Management and Automatic Reference Counting (ARC)
18 Copying Objects
19 Archiving
Part III Cocoa, Cocoa Touch, and the iOS SDK
20 Introduction to Cocoa and Cocoa Touch
21 Writing iOS Applications
Appendixes
A Glossary
B Address Book Program Source Code
1 Introduction 1
What You Will Learn from This Book 2
How This Book Is Organized 3
Support 5
Acknowledgments 5
Preface to the Fifth Edition 6
I: The Objective-C Language
2 Programming in Objective-C 7
Compiling and Running Programs 7
Using Xcode 8
Using Terminal 16
Explanation of Your First Program 18
Displaying the Values of Variables 22
Summary 25
Exercises 25
3 Classes, Objects, and Methods 27
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 52
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 89
The break Statement 91
The continue Statement 91
Summary 91
Exercises 92
6 Making Decisions 93
The if Statement 93
The if-else Construct 98
Compound Relational Tests 101
Nested if Statements 104
The else if Construct 105
The switch Statement 115
Boolean Variables 118
The Conditional Operator 123
Exercises 125
7 More on Classes 127
Separate Interface and Implementation Files 127
Synthesized Accessor Methods 133
Accessing Properties Using the Dot Operator 135
Multiple Arguments to Methods 137
Methods without Argument Names 139
Operations on Fractions 139
Local Variables 143
Method Arguments 144
The static Keyword 144
The self Keyword 148
Allocating and Returning Objects from Methods 149
Extending Class Definitions and the Interface File 151
Exercises 151
8 Inheritance 153
It All Begins at the Root 153
Finding the Right Method 157
Extension through Inheritance: Adding New Methods 158
A Point Class and Object Allocation 162
The @class Directive 163
Classes Owning Their Objects 167
Overriding Methods 171
Which Method Is Selected? 173
Abstract Classes 176
Exercises 176
9 Polymorphism, Dynamic Typing, and Dynamic Binding 179
Polymorphism: Same Name, Different Class 179
Dynamic Binding and the id Type 182
Compile Time Versus Runtime Checking 184
The id Data Type and Static Typing 185
Argument and Return Types with Dynamic Typing 186
Asking Questions about Classes 187
Exception Handling Using @try 192
Exercises 195
10 More on Variables and Data Types 197
Initializing Objects 197
Scope Revisited 200
Directives for Controlling Instance Variable Scope 200
More on Properties, Synthesized Accessors, and Instance Variables 202
Global Variables 203
Static Variables 205
Enumerated Data Types 207
The typedef Statement 211
Data Type Conversions 212
Conversion Rules 212
Bit Operators 214
The Bitwise AND Operator 215
The Bitwise Inclusive-OR Operator 216
The Bitwise Exclusive-OR Operator 217
The Ones Complement Operator 217
The Left-Shift Operator 219
The Right-Shift Operator 219
Exercises 220
11 Categories and Protocols 223
Categories 223
Class Extensions 228
Some Notes about Categories 229
Protocols and Delegation 230
Delegation 233
Informal Protocols 233
Composite Objects 234
Exercises 235
12 The Preprocessor 237
The #define Statement 237
More Advanced Types of Definitions 239
The #import Statement 244
Conditional Compilation 245
The #ifdef, #endif, #else, and #ifndef Statements 245
The #if and #elif Preprocessor Statements 247
The #undef Statement 248
Exercises 249
13 Underlying C Language Features 251
Arrays 252
Initializing Array Elements 254
Character Arrays 255
Multidimensional Arrays 256
Functions 258
Arguments and Local Variables 259
Returning Function Results 261
Functions, Methods, and Arrays 265
Blocks 266
Structures 270
Initializing Structures 273
Structures within Structures 274
Additional Details about Structures 276
Don’t Forget about Object-Oriented Programming! 277
Pointers 277
Pointers and Structures 281
Pointers, Methods, and Functions 283
Pointers and Arrays 284
Operations on Pointers 294
Pointers and Memory Addresses 296
They’re Not Objects! 297
Miscellaneous Language Features 297
Compound Literals 297
The goto Statement 298
The Null Statement 298
The Comma Operator 299
The sizeof Operator 299
Command-Line Arguments 300
How Things Work 302
Fact 1: Instance Variables Are Stored in Structures 303
Fact 2: An Object Variable Is Really a Pointer 303
Fact 3: Methods Are Functions, and Message Expressions Are Function Calls 304
Fact 4: The id Type Is a Generic Pointer Type 304
Exercises 304
II: The Foundation Framework
14 Introduction to the Foundation Framework 307
Foundation Documentation 307
15 Numbers, Strings, and Collections 311
Number Objects 311
String Objects 317
More on the NSLog Function 317
The description Method 318
Mutable Versus Immutable Objects 319
Mutable Strings 326
Array Objects 333
Making an Address Book 338
Sorting Arrays 355
Dictionary Objects 361
Enumerating a Dictionary 364
Set Objects 367
NSIndexSet 371
Exercises 373
16 Working with Files 377
Managing Files and Directories: NSFileManager 378
Working with the NSData Class 383
Working with Directories 384
Enumerating the Contents of a Directory 387
Working with Paths: NSPathUtilities.h 389
Common Methods for Working with Paths 392
Copying Files and Using the NSProcessInfo Class 394
Basic File Operations: NSFileHandle 398
The NSURL Class 403
The NSBundle Class 404
Exercises 405
17 Memory Management and Automatic Reference Counting 407
Automatic Garbage Collection 409
Manual Reference Counting 409
Object References and the Autorelease Pool 410
The Event Loop and Memory Allocation 412
Summary of Manual Memory Management Rules 414
Automatic Reference Counting 415
Strong Variables 415
Weak Variables 416
@autoreleasepool Blocks 417
Method Names and Non-ARC Compiled Code 418
18 Copying Objects 419
The copy and mutableCopy Methods 419
Shallow Versus Deep Copying 422
Implementing the Protocol 424 Copying Objects in Setter and Getter Methods 427
Exercises 429
19 Archiving 431
Archiving with XML Property Lists 431
Archiving with NSKeyedArchiver 434
Writing Encoding and Decoding Methods 435
Using NSData to Create Custom Archives 442
Using the Archiver to Copy Objects 446
Exercises 447
III: Cocoa, Cocoa Touch, and the iOS SDK
20 Introduction to Cocoa and Cocoa Touch 449
Framework Layers 449
Cocoa Touch 450
21 Writing iOS Applications 453
The iOS SDK 453
Your First iPhone Application 453
Creating a New iPhone Application Project 456
Entering Your Code 460
Designing the Interface 462
An iPhone Fraction Calculator 469
Starting the New Fraction_Calculator Project 471
Defining the View Controller 471
The Fraction Class 477
A Calculator Class That Deals with Fractions 480
Designing the User Interface 482
Summary 482
Exercises 484
Appendixes
A Glossary 487
B Address Book Example Source Code 495
TOC, 9780321887283, 11/12/2012