Preface |
|
xxv | |
Acknowledgments |
|
xxxv | |
About the Author |
|
xxxvii | |
I: Introduction to Objective-C |
|
1 | (94) |
|
1 C, the Foundation of Objective-C |
|
|
3 | (40) |
|
The Structure of a C Program |
|
|
4 | (4) |
|
|
4 | (1) |
|
|
5 | (1) |
|
|
5 | (1) |
|
Variable and Function Names |
|
|
6 | (1) |
|
|
7 | (1) |
|
|
7 | (1) |
|
|
8 | (8) |
|
|
8 | (1) |
|
|
9 | (1) |
|
|
9 | (1) |
|
|
10 | (1) |
|
|
10 | (2) |
|
|
12 | (1) |
|
|
13 | (1) |
|
|
14 | (1) |
|
|
14 | (1) |
|
|
15 | (1) |
|
|
16 | (1) |
|
|
16 | (5) |
|
|
16 | (1) |
|
|
17 | (1) |
|
Increment and Decrement Operators |
|
|
17 | (1) |
|
|
18 | (1) |
|
|
18 | (1) |
|
|
18 | (1) |
|
|
19 | (1) |
|
|
19 | (1) |
|
|
19 | (1) |
|
|
20 | (1) |
|
Other Assignment Operators |
|
|
21 | (1) |
|
Expressions and Statements |
|
|
21 | (2) |
|
|
21 | (1) |
|
|
22 | (1) |
|
|
22 | (1) |
|
|
23 | (1) |
|
|
23 | (10) |
|
|
24 | (1) |
|
|
25 | (1) |
|
|
25 | (1) |
|
|
26 | (1) |
|
|
26 | (1) |
|
|
27 | (1) |
|
|
28 | (1) |
|
|
28 | (1) |
|
|
28 | (1) |
|
|
29 | (1) |
|
|
30 | (2) |
|
|
32 | (1) |
|
|
33 | (4) |
|
|
33 | (1) |
|
|
33 | (1) |
|
|
34 | (1) |
|
|
35 | (2) |
|
Command Line Compiling and Debugging |
|
|
37 | (2) |
|
|
39 | (1) |
|
|
39 | (4) |
|
|
43 | (14) |
|
Memory Layout of a C Program |
|
|
43 | (1) |
|
|
44 | (2) |
|
|
46 | (1) |
|
|
46 | (4) |
|
|
47 | (1) |
|
|
47 | (1) |
|
|
47 | (1) |
|
|
48 | (1) |
|
|
48 | (1) |
|
|
49 | (1) |
|
|
50 | (1) |
|
The Scope of Automatic Variables |
|
|
50 | (1) |
|
Compound Statements and Scope |
|
|
50 | (1) |
|
The Scope of External Variables |
|
|
51 | (1) |
|
|
51 | (3) |
|
|
54 | (1) |
|
|
55 | (2) |
|
3 An introduction to Object-Oriented Programming |
|
|
57 | (18) |
|
Object-Oriented Programming |
|
|
57 | (3) |
|
|
58 | (1) |
|
|
58 | (1) |
|
|
58 | (1) |
|
|
59 | (1) |
|
|
60 | (1) |
|
What Is the Point of an Object-Oriented Language? |
|
|
60 | (1) |
|
An Introduction to Objective-C |
|
|
60 | (8) |
|
|
61 | (3) |
|
|
64 | (1) |
|
Messaging (Invoking a Method) |
|
|
64 | (2) |
|
Class Objects and Object Creation |
|
|
66 | (1) |
|
|
67 | (1) |
|
|
68 | (6) |
|
|
68 | (1) |
|
|
68 | (1) |
|
|
69 | (1) |
|
|
69 | (1) |
|
|
69 | (1) |
|
|
70 | (2) |
|
|
72 | (1) |
|
|
72 | (2) |
|
|
74 | (1) |
|
4 Your First Objective-C Program |
|
|
75 | (20) |
|
|
75 | (4) |
|
Objective-C Program Structure |
|
|
79 | (4) |
|
Build and Run the Program |
|
|
81 | (2) |
|
An Object-Oriented "Hello World" |
|
|
83 | (7) |
|
|
84 | (2) |
|
|
86 | (4) |
|
|
90 | (2) |
|
Build and Run the Program |
|
|
91 | (1) |
|
|
92 | (1) |
|
|
92 | (3) |
II: Language Basics |
|
95 | (200) |
|
|
97 | (22) |
|
|
97 | (3) |
|
|
97 | (1) |
|
|
98 | (2) |
|
|
100 | (3) |
|
|
101 | (2) |
|
|
103 | (8) |
|
|
103 | (1) |
|
|
104 | (1) |
|
|
105 | (1) |
|
Overriding and Messages to super |
|
|
105 | (2) |
|
|
107 | (2) |
|
Methods with the Same Name |
|
|
109 | (1) |
|
Dynamic and Static Typing |
|
|
110 | (1) |
|
|
111 | (2) |
|
|
113 | (1) |
|
|
114 | (1) |
|
Introspection and Other Runtime Fun |
|
|
115 | (2) |
|
|
117 | (1) |
|
|
117 | (2) |
|
|
119 | (30) |
|
|
119 | (4) |
|
|
119 | (2) |
|
|
121 | (1) |
|
The implementation Section |
|
|
122 | (1) |
|
|
122 | (1) |
|
|
123 | (8) |
|
|
124 | (1) |
|
An Example of Subclassing |
|
|
124 | (4) |
|
|
128 | (1) |
|
A Class Hierarchy Example |
|
|
128 | (1) |
|
|
129 | (2) |
|
|
131 | (8) |
|
|
131 | (1) |
|
|
132 | (7) |
|
|
139 | (2) |
|
|
141 | (5) |
|
|
142 | (1) |
|
Mutable and Immutable Copies |
|
|
142 | (1) |
|
Implementing Copying in Your Own Classes |
|
|
143 | (3) |
|
|
146 | (1) |
|
|
146 | (3) |
|
|
149 | (18) |
|
|
149 | (4) |
|
|
150 | (2) |
|
|
152 | (1) |
|
|
153 | (5) |
|
|
153 | (2) |
|
|
155 | (1) |
|
|
156 | (2) |
|
Mimicking Class Variables |
|
|
158 | (5) |
|
|
163 | (1) |
|
|
164 | (3) |
|
|
167 | (14) |
|
|
168 | (1) |
|
|
168 | (1) |
|
|
169 | (1) |
|
|
169 | (1) |
|
|
170 | (1) |
|
|
170 | (1) |
|
|
171 | (1) |
|
|
172 | (3) |
|
Memory Management for Core Foundation Objects |
|
|
173 | (1) |
|
|
174 | (1) |
|
|
175 | (1) |
|
|
176 | (1) |
|
Other Apple-Supplied Frameworks |
|
|
176 | (1) |
|
|
177 | (1) |
|
|
178 | (1) |
|
|
179 | (2) |
|
9 Common Foundation Classes |
|
|
181 | (28) |
|
Immutable and Mutable Classes |
|
|
181 | (1) |
|
|
182 | (1) |
|
|
183 | (5) |
|
|
184 | (2) |
|
C String to NSString and Back |
|
|
186 | (1) |
|
|
187 | (1) |
|
|
187 | (1) |
|
|
188 | (5) |
|
|
188 | (3) |
|
|
191 | (1) |
|
|
192 | (1) |
|
|
193 | (2) |
|
|
195 | (1) |
|
|
196 | (1) |
|
|
196 | (1) |
|
|
197 | (1) |
|
|
197 | (1) |
|
Objective-C Literals and Object Subscripting |
|
|
198 | (6) |
|
|
198 | (1) |
|
|
199 | (1) |
|
|
199 | (1) |
|
|
200 | (2) |
|
|
202 | (1) |
|
Adding Subscripting to Your Own Classes |
|
|
203 | (1) |
|
|
204 | (2) |
|
Geometry Structures on iOS |
|
|
206 | (1) |
|
|
206 | (1) |
|
|
207 | (2) |
|
10 Control Structures in Objective-C |
|
|
209 | (24) |
|
|
209 | (4) |
|
Testing Objects for Equality |
|
|
211 | (2) |
|
for Statements and Implicit Loops |
|
|
213 | (2) |
|
|
213 | (1) |
|
|
214 | (1) |
|
Implicit Loops with Blocks |
|
|
214 | (1) |
|
while Statements and NSEnumerator |
|
|
215 | (2) |
|
Modifying a Mutable Collection While Enumerating |
|
|
216 | (1) |
|
|
217 | (3) |
|
An Example Using Fast Enumeration |
|
|
220 | (3) |
|
|
223 | (6) |
|
Throwing Your Own Exceptions |
|
|
224 | (1) |
|
|
225 | (1) |
|
Nested Exception Handling |
|
|
226 | (1) |
|
|
227 | (1) |
|
Should You Use Exceptions? |
|
|
228 | (1) |
|
|
229 | (1) |
|
|
230 | (3) |
|
11 Categories, Extensions, and Security |
|
|
233 | (20) |
|
|
233 | (5) |
|
Overriding Methods with Categories |
|
|
236 | (1) |
|
Other Uses for Categories |
|
|
237 | (1) |
|
|
238 | (2) |
|
|
240 | (2) |
|
Instance Variable Scope (Access Control) |
|
|
242 | (1) |
|
Hiding Your Instance Variable Declarations |
|
|
243 | (3) |
|
Access Control for Methods |
|
|
246 | (1) |
|
|
246 | (1) |
|
|
246 | (4) |
|
Calling C Functions from Objective-C |
|
|
250 | (1) |
|
|
250 | (1) |
|
|
250 | (1) |
|
|
250 | (1) |
|
|
251 | (1) |
|
|
251 | (2) |
|
|
253 | (26) |
|
Accessing Instance Variables Outside of an Object (Don't Do It) |
|
|
254 | (1) |
|
Declaring and Implementing Accessors |
|
|
255 | (3) |
|
|
256 | (2) |
|
Accessors Using Properties |
|
|
258 | (2) |
|
The Instance Variable Name Can Be Different from the Property Name |
|
|
259 | (1) |
|
Synthesized Instance Variables |
|
|
260 | (1) |
|
|
261 | (1) |
|
|
262 | (1) |
|
|
262 | (1) |
|
|
262 | (1) |
|
|
263 | (1) |
|
|
263 | (3) |
|
|
264 | (1) |
|
|
264 | (1) |
|
|
265 | (1) |
|
|
265 | (1) |
|
|
266 | (1) |
|
|
266 | (1) |
|
Properties without Instance Variables |
|
|
267 | (1) |
|
Properties and Memory Management |
|
|
268 | (1) |
|
|
268 | (1) |
|
A Look Ahead at Automatic Reference Counting (ARC) |
|
|
269 | (1) |
|
|
269 | (1) |
|
|
269 | (1) |
|
|
269 | (1) |
|
Subclassing and Properties |
|
|
270 | (1) |
|
Hidden Setters for readonly Properties |
|
|
271 | (1) |
|
Properties as Documentation |
|
|
272 | (1) |
|
|
272 | (4) |
|
Dot Syntax and Properties |
|
|
274 | (1) |
|
Dot Syntax and C Structures |
|
|
274 | (2) |
|
|
276 | (1) |
|
|
277 | (2) |
|
|
279 | (16) |
|
The Rationale for Protocols |
|
|
279 | (1) |
|
|
280 | (3) |
|
|
280 | (2) |
|
|
282 | (1) |
|
|
282 | (1) |
|
|
282 | (1) |
|
|
283 | (8) |
|
|
284 | (1) |
|
|
285 | (2) |
|
|
287 | (1) |
|
|
288 | (1) |
|
|
289 | (1) |
|
Implement the Optional Methods |
|
|
290 | (1) |
|
Protocol Objects and Testing for Conformance |
|
|
291 | (1) |
|
|
291 | (1) |
|
|
292 | (1) |
|
|
293 | (2) |
III: Advanced Concepts |
|
295 | (94) |
|
14 Memory Management Overview |
|
|
297 | (4) |
|
|
298 | (1) |
|
The Solutions: Objective-C Memory Management |
|
|
299 | (1) |
|
Reference Counting (Manual Reference Counting) |
|
|
299 | (1) |
|
Automatic Reference Counting (ARC) |
|
|
300 | (1) |
|
|
300 | (1) |
|
|
301 | (24) |
|
Reference Counting Basics |
|
|
301 | (2) |
|
|
303 | (2) |
|
|
305 | (1) |
|
Taking Ownership by Copying |
|
|
306 | (1) |
|
|
306 | (2) |
|
|
308 | (6) |
|
|
309 | (1) |
|
|
309 | (1) |
|
Managing Autorelease Pools |
|
|
310 | (2) |
|
Back to Convenience Constructors |
|
|
312 | (1) |
|
|
313 | (1) |
|
Using Extra Autorelease Pools to Control Memory Usage |
|
|
313 | (1) |
|
|
314 | (1) |
|
|
314 | (2) |
|
When Retain Counts Go Bad |
|
|
316 | (3) |
|
|
317 | (2) |
|
|
319 | (2) |
|
The Final Goodbye: When Programs Terminate |
|
|
321 | (1) |
|
|
322 | (1) |
|
|
323 | (2) |
|
|
325 | (28) |
|
|
326 | (1) |
|
|
326 | (2) |
|
|
328 | (4) |
|
You Can't Invoke the Memory Management Methods Yourself |
|
|
328 | (1) |
|
|
328 | (1) |
|
Method Naming Conventions |
|
|
329 | (1) |
|
ARC Needs to See Method Declarations |
|
|
330 | (1) |
|
Obective-C Pointers and C Structs |
|
|
331 | (1) |
|
|
332 | (4) |
|
|
332 | (2) |
|
|
334 | (1) |
|
|
334 | (1) |
|
|
335 | (1) |
|
|
336 | (1) |
|
|
337 | (3) |
|
|
340 | (3) |
|
CF Objects Returned from Methods |
|
|
341 | (1) |
|
|
341 | (2) |
|
|
343 | (3) |
|
ARC and Extra Autorelease Pools |
|
|
346 | (1) |
|
|
346 | (1) |
|
|
347 | (2) |
|
|
347 | (1) |
|
|
347 | (1) |
|
|
347 | (2) |
|
ARC Uses Runtime Functions |
|
|
349 | (1) |
|
|
349 | (1) |
|
|
350 | (1) |
|
|
351 | (2) |
|
|
353 | (28) |
|
|
354 | (5) |
|
Calling a Function with a Function Pointer |
|
|
356 | (1) |
|
|
356 | (2) |
|
The Trouble with Function Pointers |
|
|
358 | (1) |
|
|
359 | (3) |
|
|
362 | (15) |
|
|
362 | (1) |
|
|
363 | (22) |
|
|
385 | |
|
|
366 | (1) |
|
|
367 | (1) |
|
Blocks Are Objective-C Objects |
|
|
367 | (1) |
|
|
367 | (1) |
|
Memory Management for Blocks |
|
|
368 | (1) |
|
|
369 | (2) |
|
|
371 | (2) |
|
|
373 | (1) |
|
|
374 | (3) |
|
|
377 | (1) |
|
Some Philosophical Reservations |
|
|
377 | (1) |
|
|
378 | (1) |
|
|
378 | (3) |
|
|
381 | (8) |
|
Enums with a Fixed Underlying Type |
|
|
381 | (3) |
|
Setting the Underlying Type |
|
|
382 | (1) |
|
|
383 | (1) |
|
|
383 | (1) |
|
Checking switch Statements with Enum Arguments |
|
|
384 | (1) |
|
Forward Declarations of Methods in the @implementation Block Are No Longer Needed |
|
|
384 | (3) |
|
|
387 | (1) |
|
|
387 | (1) |
|
|
387 | (2) |
IV: Appendices |
|
389 | (16) |
|
A Reserved Words and Compiler Directives |
|
|
391 | (2) |
|
B Toll-Free Bridged Classes |
|
|
393 | (2) |
|
|
395 | (4) |
|
Kernel and User Programs in 64-Bit |
|
|
396 | (1) |
|
Coding Differences for 64-Bit Programs |
|
|
396 | (1) |
|
|
396 | (1) |
|
Compiling for 32-Bit and 64-Bit |
|
|
397 | (1) |
|
|
398 | (1) |
|
D The Fragile Base Class Problem |
|
|
399 | (2) |
|
E Resources for Objective-C |
|
|
401 | (4) |
|
|
401 | (1) |
|
|
402 | (1) |
|
|
402 | (1) |
|
|
403 | (2) |
Index |
|
405 | |