Index
A
ABI. See Application Binary Interface (ABI)
abstract base class
hiding implementation detail, 42–47
immutable and mutable arrays, 45
subclasses inheriting from class cluster, 46
ACAccountStoreBooleanCompletionHandler type, 196
accessor methods
automatically writing, 25, 28–29
categories, 131
fine-grained control over, 31
locking for atomicity, 29–30, 32
names, 31
preventing synthesis, 29
strict naming patterns, 27
user-implemented, 29
Accounts framework, 196
addObject: method, 149
addOperationWithBlock: method, 218
alertView parameter, 49
allObjects method, 237
alloc method, 149, 151, 153–154
AND operator, 19
animation, 235
anInstanceMethod method, 190
anonymous objects and protocols, 140–142
APIs
C-level, 235
completion callback blocks, 205–206
Application Binary Interface (ABI), 27
applications, 2
controlling high memory, 174
multithreaded, 187
terminating when main threads blocked, 197
applicationWillTerminate: method, 163
ARC. See Automatic Reference Counting (ARC)
arguments and messages, 50
arrays, 243
adding number object, 149
binning objects with hash, 38
block-based enumeration, 189, 240–241
common methods for, 45
defining interface to, 46
mutable, 45
nil objects and exceptions, 9–10
placeholder arrays, 45
reversing enumeration, 240
size of, 243
arrayWithObjects: method, 10
associated objects
attaching custom data to classes, 47–50
categories, 131
keys, 48
associations, 47
asynchronous dispatch, 210
atomic attribute, 32
atomic locking and accessor methods, 29–30
attributes
memory-management semantic, 91
audio hardware, C-level API for interfacing with, 234–235
audio playback and recording, 235
AudioToolbox framework, 74
autoDictionaryGetter function, 60–61
autoDictionarySetter function, 60, 61
Automatic Reference Counting (ARC), xiii, 108
backward compatibility, 156
cleanup code during deallocation, 160
destroying objects, 167
exceptions, 105
extraneous autorelease plus immediate retain, 157–158
instance variables, 159, 160–161
lower-level C methods, 154
memory-management rules, 214
memory-management semantics of variables, 158–161
message dispatch and, 161
Objective-C objects, 231, 235, 244
ownership of objects, 244
reference counting and, 153–161
runtime component, 156
safely handling exceptions, 167
variables retaining value, 158–159
writing setter, 159
autonilling, 117
autorelease method, 146, 150–151, 154, 156–157, 184
autorelease pools, 146, 150–151, 185
draining, 151
overhead, 176
reducing high memory waterline, 173–177
scope, 174
stack, 175
threads, 173
autoreleases, collapsing, 156
@autorelease syntax, 176
__autoreleasing qualifier, 159
AVFoundation framework, 235
B
backward compatibility and Auto-matic Reference Counting (ARC), 156
barrier feature, 211
base class
defining, 43
instances, 45
beginContentAccess method, 251–252
bitwise AND operator, 19
bitwise OR operator, 19, 22, 23
block-based enumeration, 240–243
changing method signature to limit need for casting, 241–242
reversing, 242
stopping, 241
__block qualifier, 189
array enumeration, 189
asynchronous dispatch, 210
callback, 203
caret (^) symbol, 188
copies of captured variables, 191–192
direct use and deadlock, 208
flexibility, 201
function pointers, 191
Grand Central Dispatch (GCD), 216
hiding complicated type, 194
inability to cancel, 218
instance methods, 190
instance variables, 190
memory regions, 191
network fetchers, 199–200, 205
only executed once, 225
parameters, 194
pointer declaring size of, 191
queues to schedule in, 203
registering to observe notification through, 219
returning int, 188
return type, 194
running on queue, 221
runtime component, 188
self variable, 190
splitting up success and failure cases, 200–202
synchronous dispatch, 210
used as variable, 188
Boolean properties
methods, 100
specifying name of getter, 31
BOOL type, 194
breakpoints, 88
_bridge keyword, 244
_bridge_retained keyword, 244
_bridge_transfer keyword, 244
buffer parameter, 99
business logic, decoupling data from, 115
buttons, 42
buttonWithType: method, 42
C
C++
exceptions, 165
function names, 96
NS_OPTIONS enumeration, 22
scope of instance variables, 26
system libraries, 137
caches
automatically removing objects, 251
order objects evicted, 249
retaining keys, 248
thread safe, 248
URL retrieved as cache key, 250
caching
delegate responding to selectors, 122–123
methods delegates implement, 122
optimization of, 121
CALayer class, 62
callback blocks, 203
camel casing names, 95
cancel method, 218
capitalizedString selector, 63–64
catch block and exceptions, 166
categories, 115
accessor methods, 131
associated objects, 131
breaking class implementation into segments, 123–127
inability to synthesize variables, 131
loading, 252
load method, 253
prefixing names, 74
prefixing third-party names, 127–129
CFArray class, 243
CFArrayGetCount function, 243
CFArrayRef class, 243
_CFArray struct, 243
CFDictionaryKeyCallBacks struct, 245
CFDictionary type, 244
CFDictionaryValueCallBacks struct, 245
CFEqual class, 247
CFHash class, 247
CFMutableDictionary type, 244
CFNetwork framework, 234
CFString class, 234
CFURLRequest data structure, 126
CGFloat scalar type, 30
CGPoint data structure, 235
CGRect data structure, 3–4, 235
CGSize data structure, 235
Clang compiler, 153
blocks and, 188
class_addMethod runtime method, 60
Class Cluster pattern, hiding implementation detail, 42–47
adding concrete implementation, 46
checking class of instance of, 46
collection classes as, 45
Factory pattern, 44
member objects, 45
subclasses inheriting from abstract base class, 46
class-continuation category, 7–8, 30, 130
classes implementing protocol, 117–118
data definitions, 135
hiding implementation detail, 133–140
implementation file definition, 133–140
instance variables, 27, 122, 133–134
mixing Objective-C and C++ code, 135–137
objects conforming to private protocols, 138–139
properties, 135
classes, 25
attaching custom data with associated objects, 47–50
breaking implementation into segments, 123–127
categories adding methods to, 127–128
checking instance of class cluster, 46
collections, 243
comparing for equality, 37
copying mutable and immutable variants, 112–113
custom, 12
defining blocks in instance method, 190
delegates, 7
designated initializers, 78–84
documentation, 44
dynamically adding method for unknown selector, 55–56
equality-checking methods, 36
hooking into forwarding paths, 55
initializers, 78
instance variables, 26, 28, 130
internal methods, 102
limiting imports to, 6
literals and, 12
metadata, 68
multiple network fetchers, 199
namespace clashes, 100
not understanding messages, 54–62
parent class, 68
properties, 89
protocols, 120
referring to each other, 6
retrieving methods for selectors, 64
shared-instance method, 225
system-supplied, 129
translation unit, 14
type definition, 196
class_getInstanceMethod method, 64, 66
class-specific equality methods, 39–40
Class type, 68
C-level facilities for talking to networks, 234
close method, 163
protocol language feature, 116
two-letter prefixes, 74
UI work on main thread, 202
Cocoa Touch, xi, xiii, 173, 234
UI work on main thread, 202
code
accessing from multiple threads, 208
calculating offset at compile time, 27
conventions for generating, 27
documenting interface to, 115
incompatibility at runtime, 27
memory leaks, 105
mixing Objective-C and C++ and class-continuation category, 135–137
readable, 18
reducing separation with handler blocks, 197–203
segmenting into functional areas, 126
thread-safe single-time execution, 225–226
waiting unnecessarily on lock, 208
collection classes
as class clusters, 45
mutable or immutable properties, 92–94
shallow copying, 113
collections, 234
adding objects, 40
classes for, 243
custom memory-management semantics, 243–247
guaranteeing types of objects in, 242
retrieving objects from, 69–70
comparing objects for equality, 36–42
compiler
deciding which code executes, 1–2
instance variables, 28
Objective-C++ mode, 167
compiler flag enabling exception-safe code, 105
_completion function, 76
completion handler, 203
instance variables, 203
other classes creating, 205
completion-handler block, 205–206
completionHandler property, 205, 207
concurrent threads, 223
reads as normal blocks and writes as barrier blocks, 212
connections handling error representing what happened, 107
connectionWithIdentifier: method, 142
constants
global, 17
global symbol table, 15
header file declaration, 15, 17
implementation file definition, 13, 15, 17
translation unit, 17
const qualifier, 14
containers with mutable classes and equality, 40–42
copyDescription callback, 247
copy helper, 191
copying
designated initializer, 110, 112
mutable and immutable variants of class, 112–113
copy message, 192
copy method, 109–110, 112–113, 154
copyWithZone: method, 109–110, 112–114, 247
CoreAnimation framework, 137, 235
CALayer class, 62
CoreData databases, 142
CoreData framework, 235
accessing properties of NSManagedObjects class, 55–56
anonymous objects, 142
CoreFoundation framework, 234, 243
zombie objects, 179
CoreFoundation objects
cleaning up, 160
releasing references, 162
CoreGraphics framework, 235
CoreText framework, 235
countByEnumeratingWithState: method, 239
count method, 46
_cppClass instance variable, 136
C programming language
accessing stack-allocated struct member, 28
memory model, 2
Objective-C as superset of, 2, 4
prefixing function names, 77
seamless casting data structures with Objective-C objects, 234
custom
classes, 12
data attaching to classes with associated objects, 47–50
memory-management semantics, 243–247
.cxx_destruct method, 160–161, 162
D
dangling pointer, 149
data
decoupling from business logic, 115
encapsulating, 130
equality, 40
databases, interfaces for persisting objects, 235
data source protocol, 120
data structures, 2D rendering, 235
_dateOfBirth instance variable, 27
deadlocks
dispatch queues, 228
getters, 227
synchronization blocks, 208
dealloc method, 154, 160–165, 181
debugDescription method, 88–89
debugger, invoking print-object command within, 88–89
debugging
breakpoints, 88
dispatch_get_current_queue function, 231
memory-management problems, 177–183
printing object to inspect during, 84–89
public and private methods, 102
splitting classes into categories, 126–127
deep equality, 40
#define preprocessor directive versus typed constants, 12–17
#define preprocessor macros, 20–22
Delegate pattern, 115–123, 171, 197
delegate protocols, 7, 117–118, 197–198
errors, 107
delegates
anonymous, 141
caching methods, 122
classes implementing, 7
implementing, 117
introspection, 119
list views, 120
methods, 119
network fetchers, 119
nonowning relationship, 117
obtaining information from, 119–120
passing errors to, 107
property definition, 140
responding to selectors, 121–123
task completion notification, 197–198
description message, 84
description method
piggybacking on NSDictionary class’s implementation, 86–87
descriptor variable, 191
designated initializers, 78–84
EOCRectangle class, 81
overriding superclass, 80
Rectangle object, 79
superclass, 80
destructor function, 231
device orientations supported by views, 20
dictionaries, 243
block-based enumeration, 241
CoreFoundation framework, 244–245
within description method, 86–87
Foundation framework, 244
immutable variants, 12
keys, 11–12, 48, 237–238, 241, 245–247
memory-management semantics, 140–141
mutable arrays and, 11
nil values and exceptions, 11
retaining keys, 248
storing downloaded images, 248
subscripting, 11
user-created pruning code, 248
dispatch_after argument, 216
dispatch_apply function, 223–224
dispatch_async argument, 216, 220
dispatch_function_t type, 231
dispatch_get_current_queue function, 226–231
dispatch_group_async function, 220
dispatch_group_enter function, 220
dispatch_group_leave function, 220
dispatch_group_notify function, 221
dispatch groups
associating tasks with, 220
timeout value, 221
tracking tasks submitted to serial queues, 222–223
dispatch_group_wait function, 221
dispatch_once function, 217, 225–226
dispatch_once_t type, 225
deadlock, 228
versus locks for synchronization, 208–213
dispatch_sync argument, 216
dispose helper, 191
doADaysWork, 45
documentation, 44
doesNotRecognizeSelector: exception, 55, 57
“Don’t Repeat Yourself” (DRY) principle of software development, 219
doSomething: method, 108
doSomethingThatMayThrow object, 166
doSomethingThatUsesItsInternalData method, 256
doSomethingWithInit: method, 174
doSomethingWithObject: method, 186
DRY. See “Don’t Repeat Yourself” (DRY) principle of software development
on top of dynamic binding, 213–214
@dynamic keyword, 29
dynamic libraries, 233
dynamic method resolution, 55–56, 59–62
@dynamic properties, 55–56, 59–62
dyspatch_sync function, 227–228
E
else statement, 192
encapsulating data, 130
endContentAccess method, 251–252
enumerateKeysAndObjectsUsingBlock: method, 242
enumerateKeysAndObjectsWithOptions: method, 242
enumerateObjectsUsingBlock: method, 189
enumerateObjectsWithOptions: method, 242
enumerating collections
block-based enumeration, 240–243
dictionaries, 237
Objective-C 1.0 enumerations, 237–239
sets, 237
enumerations
adding typedef, 18
behavior of, 242
forward declaring types, 18
grouping error status codes, 22
next object in, 238
readable code, 18
switch statement, 23
types, 18
value backing, 18
EOCAnimatedView class, 16
EOCAnimatedView.m file, 14, 16
EOCAutoDictionary class, 59–60, 62
EOCBaseClass class, 255
EOCClassA class, 169, 171, 253, 256
EOCClassB class, 169–171, 253, 256
shared-instance method, 225
EOCClass.h header file, 136–137
EOCClass instance, 205
EOCClass.mm implementation file, 136
EOCConnectionStateConnected enumeration, 19
EOCConnectionStateDisconnected enumeration, 19
EOCConnectionState enumeration, 18, 20
EOCDatabaseConnection protocol, 141–142
EOCDataModel object, 116
EOCDelegate object, 140
EOCDog class, 37
EOCDrawable protocol, 7
EOCEmployer.h header file, 5–6
EOCImageTableView class, 101
EOCLibrary library, 77
eoc_myLowercaseString method, 65
EOCNetworkFetcher class, 117, 119, 122
EOCNetworkFetcher object, 116, 207
EOCObject class and private methods, 102–103
EOCPermittedDirection enumeration, 22
EOCPerson class, 5, 31–32, 35, 39–40, 88, 93–94, 110–111, 114, 124–125, 130, 138–139, 175
class-continuation category, 133
implementation file, 6
EOCPerson.h header file, 4–6, 126
EOCPointOfInterest class, 90, 91–92
EOCRectangle class, 78–79, 80, 96–97
designated initializers, 81, 82–84
EOCReleaseCallback function, 247
EOCRetainCallback function, 247
EOCSecretDelegate protocol, 138–139
EOCSmithPerson class, 37
EOCSomeBlock type, 195
EOCSoundPlayerCompletion completion handler, 77
EOCSSmithPerson subclass, 35
EOCStringConstant constant, 15
EOCSubClass class, 255
EOCSuperSecretClass class, 134–135
EOCTheClass class, 73
EOCViewClassAnimationDuration constant, 13
equal callback, 247
equality
data, 40
deep versus shallow, 40
mutable classes in containers, 40–42
strings, 99
equality-checking methods, 36–37
equality (==) operator, 36, 70
error codes as enumeration type, 108–109
error domains
defining what error occurred, 106–107
libraries, 109
error parameter, 108
error status codes
grouping, 22
evictsObjectsWithDiscardedContent property, 251
exceptions, 104
Automatic Reference Counting (ARC), 105
C++, 165
catch block, 166
causing application to exit, 105
code catching and handling, 165
Key-Value Observing (KVO), 165
memory leaks, 105
nil values in dictionaries, 11
Objective-C, 165
throwing in overridden methods, 105–106
try block, 166
zombie objects, 178
exception-safe code, 105
extern keyword, 15
F
factory method, 98
Factory pattern, 44
fast enumeration, 236, 239–240
_fetchedData instance variable, 205
file descriptors, 162
_firstName instance variable, 26–27, 28
-fobjc-arc-exceptions flag, 105, 167–168
_foo instance variable, 150
foo setter accessor, 150
versus block enumeration, 236–243
enumerating collections, 236–237
forward declaring, 8
classes, 6
imports, 7
types for enumerations, 18
_forwarding_ function, 181–182
forwardingTargetForSelector: method, 56
forwardInvocation:method, 57
dictionaries, 244
importing entirety, 5
loading, 253
NSCache class, 248
NS class prefix, 234
NSTimer class, 258
string processing, 234
zombie objects, 179
Foundation.h header file, 5
frameworks
See also system frameworks
base header file, 5
protocol language feature, 116
third-party, 233
friendship category, 130
_friends instance variable, 111–112
friends property, 94
full forward mechanism, 57
function pointers, 191
functions, 2
calling versus messaging, 1
polymorphism, 2
2D rendering, 235
unknown until runtime, 51
G
Galloway Web site, xii
GCD. See Grand Central Dispatch (GCD)
getCharacters:range: method, 99
getter=<name> attribute, 31
getters
_block syntax, 210
deadlock, 227
properties, 30
running concurrently, 210
serial synchronization queue, 210
specifying name of, 31
global constants, 17
global symbol table, 15
global variables
defining source of error, 106
name conflicts, 74
Grand Central Dispatch (GCD), xi, xiii, 187
barrier feature, 211
dispatch queues, 217
inability to cancel blocks, 218
versus performSelector: method and friends, 213–217
queue currently being executed, 226–231
queue priorities, 218
queue-specific data functions, 230–231
synchronization, 217
graphics, rendering, 235
groups, incrementing or decrementing number of tasks, 220
H
handler blocks, reducing code separation with, 197–203
hash callback, 247
hash tables, hash as index in, 38
hasPrefix: method, 99
extern keyword, 15
protocols, 7
superclass, 7
heap
allocating and de-allocating memory, 2–4
copying blocks from stack, 192
managing memory, 3
heap-allocated memory, cleaning up, 160
height instance variable, 96
_height instance variable, 80, 84
helpers, backward compatibility, 20–22
.h file extension, 4
high memory, reducing waterline, 173–177
HTTP, obtaining data from server, 126
HTTP requests to Twitter API, 74
HTTP-specific methods, 126
I
IDE. See Integrated Development Environment (IDE)
identifier property, 92
id<EOCDelegate> type, 140
id<NSCopying> type, 141
images, caching from Internet, 248–252
immutable arrays, 45
immutable collection-class properties, 92–94
immutable variants, 12
implementation
class-continuation category hiding detail, 133–140
hiding with class clusters, 42–47
instance variables, 27
class-continuation category, 133–140
#import preprocessor directive, 7
imports, forward declared, 7
#include preprocessor directive, 7
indexes for arrays, 12
inheritance, hierarchies with equality, 37
initialize method
called lazily, 254
inheritance, 255
lean implementation of, 255–256
runtime in normal state, 254
thread-safe environment, 254
initializers
classes, 78
instance variables, 35
methods, 35
naming, 78
throwing exceptions, 81
initWithDimension: method, 81
initWithFormat: method, 151
initWithInt: method, 149
initWithSize: method, 97
initWithTimeIntervalSinceReferenceDate: designated initializer, 78
initWithWidth:andHeight method, 81
in keyword, 239
inline blocks, 189
instances
base class, 45
instance variables, 68
metadata, 68
value passed in, 106
instance variables, 25
accessor methods, 34
Automatic Reference Counting (ARC) handling, 160–161
bitfield, 122
blocks, 190
class-continuation category, 27, 122, 133–134
cleanup code during deallocation, 160
completion handler, 203
directly accessing internal, 33–36
documenting not exposed publicly, 133–140
externally accessing, 33
getters, 25
inability to be synthesized by category, 131
instances, 68
memory-management semantics, 158–160
network fetchers stored as, 199
only known internally, 134
pointer to instance, 136
private, 90
queue usage, 227
runtime, 27
safe setting of, 159
scope, 26
setting to 0 (zero), 79
as special variables held by class objects, 27
subclasses, 46
superclass, 35
synthesis, 29
integers
defined at compile time, 257
parsing strings as, 98
Integrated Development Environment (IDE), 195
interface
classes implementing protocol, 117
documenting code, 115
Internet, caching images from, 248–252
classes, 92
delegates, 119
objects, 94
retrieving objects from collections, 69–70
intValue method, 98
invoke variable, 191
blocked UI thread, 187
blocks, 188
main thread, 221
mapping functionality, 235
method called on application termination, 163
operation queues, 217
social networking facilities, 235–236
UI framework, 78
UI work performed on main thread, 226
iOS applications
UIApplication object, 147
iOS 5.0 SDK, 74
isa instance variable, 181
isCancelled property, 218
isContentDiscarded method, 250
isEqual: method, 36–39, 40, 42, 70, 247
isEqualToArray: method, 39
isEqualToDictionary: method, 39
isEqualToString: method, 36, 39, 99
isFinished property, 218
island of isolation, 152
isMemberOfClass: method, 69
J
Java
exceptions, 104
function names, 96
instance variables scope, 26
interfaces, 115
K
kAnimationDuration constant, 14
kAnimationDuration global variable, 13
keys
associated objects, 48
retain and release callbacks, 247
static global variables, 48
Key-Value Coding (KVC), 92
Key-Value Observing (KVO)
exceptions, 165
operation properties, 218
properties, 165
KVC. See Key-Value Coding (KVC)
L
_lastName instance variable, 28
lastObject method, 46
length method, 98
lengthOfBytesUsingEncoding method, 98–99
libdispatch, xi
libraries
duplicates, 74
error domain, 109
Private category, 127
retain count, 185
list views, 120
literals, 12
localizedString method, 100
localizedStringWithFormat: method, 98
local variables, 14
breaking retain cycles, 160
memory-management semantics, 158–160
locks
Grand Central Dispatch (GCD), 208–213
recursive, 208
threads taking same lock multiple times, 208
lowercaseString method, 98
lowercase strings, 98
lowercaseString selector, 55, 63, 64–66
M
blocked UI thread, 187
blocks, 188
method called on application termination, 163
NSApplication object, 147
operation queues, 217
social networking facilities, 235–236
main interface, defining properties, 132
main thread, 173
blocked terminating application, 197
UI work in Cocoa and Cocoa Touch, 202
malloc() function, 160
MapKit framework, 235
MKLocalSearch class, 202
memory
allocated on stack for blocks, 192
heap-space allocation for objects, 2–3
releasing large blocks of, 162
reusing, 177
segmenting into zones, 109
code safe against, 105
multiple objects referencing each other cyclically, 152
object not released, 153
retain cycles, 169
memory management
Automatic Reference Counting (ARC), 155–156
debugging problems with zombies, 177–183
property accessors, 150
reference counting, 145–152, 183
memory-management methods, 2
illegally calling, 154
overriding, 161
memory-management model, 168
memory-management semantics, 35
bypassing, 34
objects, 47
memory regions, 191
message dispatch and Automatic Reference Counting (ARC), 161
message forwarding, 52
dynamic method resolution, 55–56, 59–62
full forward mechanism, 57
replacement receiver, 55–57, 59
selector, 57
messageName selector, 52
message object, 153
messages
arguments, 50
description message, 84
floating-point values, 53
method lookup, 103
names, 50
object not understanding, 54–62
objects responding to all, 103
structs, 53
superclass, 53
unhandled, 57
values, 50
versus function calling, 1
structure, 1
metaclass, 68
metadata, 68
method dispatch, 34
method dispatch system, 103
method_exchangeImplementations method, 64
method-naming rules and Automatic Reference Counting (ARC), 154–158
methods
arrays, 45
Automatic Reference Counting (ARC), 154–158
autoreleasing objects, 156–157
badly-named, 97
Boolean indicating success or failure, 107–108
Boolean properties, 100
causing action to happen on object, 100
as C function, 53
class-continuation category, 133–134
class methods, 64
debugging opaque methods, 62–66
delayed execution of, 213
delegate methods, 119
delegates, 122
documenting not exposed publicly, 133–140
dynamically adding for unknown selector, 55–56
dynamic binding, 52
exchanging implementations, 63–66
get prefix, 100
HTTP-specific, 126
introspection, 46
memory-management, 2
mutable arrays, 45
newly created value, 100
nil / 0, 106
not reentrant, 227
NSObject protocol, 85
out-parameter passed to, 107–108
owned by caller, 154
return type, 98
system-supplied class, 129
types, 100
well-named, 97
.m file extension, 4
MKLocalSearch class, 202
multithreaded applications, 187
multithreading, 187
mutable arrays, 45
dictionaries and, 11
methods, 45
runtime, 257
subscripting, 11
mutable classes, 89
mutable collection-class properties, 92–94
mutableCopy method, 112–113, 154
mutableCopyWithZone: method, 112
mutable variants, 12
_myFirstName instance variable, 28
_myLastName instance variable, 28
N
name clashes, 74
name conflicts, 74
named constants as error status codes, 17–24
naming
camel casing, 95
delegate protocol, 117
methods rules in Automatic Reference Counting (ARC), 154–158
prepositions in, 95
properties, 98
superclass, 101
variables, 95
nesting autorelease pools, 174–175
networkFetcher:didFailWithError: method, 118
networkFetcher:didUpdateProgressTo: method, 121
adding itself to global collection, 206
deallocating, 207
delegates, 119
multiple, 199
retaining block, 205
stored as instance variables, 199
networks, C-level facilities for talking to, 234
new method, 154
newSelector selector, 64
NeXTSTEP operating system, 234
NIBs XML format, 82
nonatomic attribute, 29, 32–33
nonfatal errors indicators, 106
nonfragile Application Binary Interface (ABI), 27
non-Objective-C objects
cleaning up, 160
releasing references, 162
notifications, 15
notify function, 221
NSApplicationDelegate object, 163
NSApplication object, 147
NSArray class, 8, 12, 39–40, 45–46, 86, 89, 100, 112, 129, 234, 243
NSArray collection, 236
NSAutoReleasePool object, 176
NSBlockOperation class, 218–219
NSCache class versus NSDictionary class, 248–252
NSCalendar class, 132
_NSCFNumber type, 55
NSCopying object, 141
NSCopying protocol, 109–114, 247
NSDate class, 78
NSDictionary class, 8, 12, 39, 140, 234, 241
piggybacking on description method of, 86–87
retaining keys, 248
NSDictionary collection, 236
NSDictionary object, 48, 231, 246
NSDiscardableContent protocols, 250
NSEnumerationConcurrent option, 242
NSEnumerationOptions type, 242
NSEnumerationReverse option, 242
NSEnumerator class, 236
NSFastEnumeration protocol, 239–240
NSFetchedResultsController class, 142
NSFetchedResultsSectionInfo protocol, 142
NSHTTPURLRequest category, 126
NSInteger scalar type, 30
NSLinguisticTagger class, 234
NSManagedObject class, 29
NSMutableArray class, 45, 100, 112
NSMutable arrays, 41
NSMutableCopying protocol, 112
NSMutableDictionary class, 247
NSMutableString subclass, 30
NSMutableURLRequest category, 126
NSMutableURLRequest class, 126
NSNotificationCenter API, 15, 203
NSNotificationCenter class, 162, 219
NSObject class, xi, 37, 54–55, 67, 79, 85, 109, 213, 233, 234, 247
NSObject object, 181
NSObject protocol, 36–37, 67, 85, 88, 146, 183
NSOperationQueue class, 203, 217–218
NSPurgeableData class, 250
NSPurgeableData objects, 251
NSRecursiveLock class, 208
NSSet collection, 236
NSString class, 8, 12, 36, 38–39, 128–129, 153, 253
NSString global constant, 108
NSString object, 8, 15, 151, 174, 185
NSString type, 30
NSTimeInterval type, 13
NSUInteger type, 22
NSURLConnection class, 107, 234
NSURLConnectionDelegate method, 107
NSURLErrorDomain domain, 106
NSURLRequest class, 126
_NSZombie_ class, 181
NSZombieEnabled environment variable, 178, 181
_NSZombie_EOCClass object, 180
_NSZombie_OriginalClass object, 181
_NSZombie_ template class, 180
NSZone class, 109
null pointer, 108
number object, 149
O
OBJC_ASSOCIATION_ASSIGN type, 47
OBJC_ASSOCIATION_COPY_NONATOMIC type, 47
OBJC_ASSOCIATION_COPY type, 47
objc_AssociationPolicy enumeration, 47
OBJC_ASSOCIATION_RETAIN_NONATOMIC type, 47
OBJC_ASSOCIATION_RETAIN type, 47
objc_autoreleaseReturnValue function, 157–158
objc_duplicateClass() function, 181
objc_getAssociatedObject method, 47
objc_removeAssociatedObjects method, 48
objc_retainAutoreleaseReturnValue function, 157
objc_retain method, 154
objc_setAssociatedObject method, 47
ObjectA object, 146
ObjectB object, 146
ObjectC object, 146
object_getClass() runtime function, 179
object graph, 146, 149–150, 168
_object instance variable, 158
Objective-C
exceptions, 165
garbage collector, 145
messages and zombie objects, 179–180
messaging structure, 1
runtime component, 2
square brackets, 1
Objective-C 1.0 enumerations, 237–239
Objective-C++ mode, 167
Objective-C objects
Automatic Reference Counting (ARC), 235
seamless casting with C data structures, 234
what to do with, 244
objects, 25
accessing data encapsulated by, 27
associated with objects, 47
becoming related to each other, 146
binning into arrays with hash, 38
cleaning up observation state, 162–165
conforming to private protocols, 138–139
deallocating, 146, 149, 162, 169
declaring, 2
description message, 84
destruction and exceptions, 166–167
dictionaries, 11
equality, 70
equality-checking methods, 36–37
exceptions, 105
heap-space allocated memory, 2–3
inconsistent internal data, 81
instance variables, 25
introspection, 94
invalid references to, 146
memory, 2
memory-management semantics, 47
memory regions, 191
messaging, 25
methods as C functions, 53
multiple referencing each other cyclically, 152
nonvalid, 149
not understanding message, 54–62
object graph, 150
operation queues as, 217
overhead, 4
owning other objects, 146, 148
printing to inspect while debugging, 84–89
reference counting, 173
responding to messages, 103
retaining, 156
with same hash, 42
serializing with NIBs XML format, 82
sets, 237
stack-allocated memory for, 2, 3
storage policies, 47
synchronized against self variable, 208
talking to each other, 115–123
variable of Class type, 68
variables, 2
when application terminates, 163
zombie objects, 179
object1 variable, 10
object2 variable, 10
object3 variable, 10
obj_msgSend_fpret function, 53
obj_msgSend_stret function, 53
obj_msgSendSuper function, 53
opaque methods, debugging, 62–66
opaqueObject property, 60
open: method, 163
@optional keyword, 118
options
overriding methods, 129
P
parameters
pointers to structures, 245
parsing strings, 234
performSelector: method versus Grand Central Dispatch (GCD), 213–217
performSelector:withObject:withObject: method, 215
personWithName: method, 156
placeholder arrays, 45
platform scaling and dispatch groups, 220–224
po command, 88
pointers
comparing for equality, 37
dangling pointer, 149
denoting objects, 2
nilling out, 149
to struct, 243
pointerVariable variable, 67
polymorphism, 2
prefixing
category third-party names, 127–129
preprocessor directive, 13
printGoodbye() function, 51
printHello() function, 51
print-object command invoking in debugger, 88–89
Private category, 127
private instance variables, 90
programmer error, 164
avoiding in categories, 130–133
class-continuation category, 135
classes, 89
collection-class mutable or immutable, 92–94
comparing for equality, 37
in definition of object, 27
encapsulating data, 130
getter methods, 30
instance variables, 33–36, 35, 92
Key-Value Coding (KVC), 92
Key-Value Observation (KVO), 165
main interface definition, 132
memory-management semantics, 30–31, 34, 91, 131
naming, 98
nonowning relationship, 30
operations, 218
owning relationship, 30
setter methods, 30
single words as, 98
value potentially unsafe, 171
weak, 117
@property attribute, 47
property dot syntax, 34
@property syntax, 27
protocol language feature, 116
protocols, 115
acquiring data for class, 120
declaring conformance, 8
header files, 7
hiding implementation detail in API, 141
methods for database connections, 141–142
namespace clashes, 100
objects conforming to private, 138–139
public interface declaring instance variables, 25–26
pure C functions name conflicts, 74
Q
QuartzCore framework, 235
querying structures, 122
_queueA variable, 228
queue barrier, 211
_queueB variable, 228
queues
associating data as key-value pair, 230–231
currently being executed, 226–231
instance variables, 227
many reads and single write, 211–212
priorities, 218
reading and writing to same, 209–210
scheduling callback blocks, 230
synchronization of property, 229
R
read-only properties, 30, 32, 89, 90–91
class-continuation category and setting internally, 137–138
readwrite attribute, 30
read-write property, 89
rectangle class, 7
Rectangle object, 79
recursive locks, 208
reference-counted objects, 192
reference counting, xiii, 2–3, 3, 145–152, 173, 220–221
Automatic Reference Counting (ARC) and, 153–161
autorelease pools, 150–151, 173–177
catching exceptions, 168
destruction of objects, 166–167
indicating problem areas, 153–161
manually releasing objects, 162
memory management, 183
refactoring, 168
retain cycles, 152
zombie objects, 179
reference-counting architecture, 168
references, releasing, 162–165
release method, 146, 154, 159, 184
removeFriend: method, 94
replace function, 95
replacement receiver, 55–57, 59
_resetViewController method, 104
resolveClassMethod: method, 55
resolveInstanceMethod: method, 56
respondsToSelector: method, 119
results controller, 142
retain count, 146, 173, 183, 185–186
autorelease pool, 146
balancing, 151
changing, 184
current, 183
at given time, 184
incrementing and decrementing, 146, 150
inspecting, 146
at least 1, 149
retainCount method, 146
Automatic Reference Counting (ARC), 183–184
retain cycles, 50, 152, 205–207
detecting, 169
memory leaks, 169
objects referencing each other, 168–169
retain method, 146, 154, 156–157, 159, 184
root classes, 85
run loops and timers, 258
runtime, 25
dealloc method, 162
deciding which code executes, 1
as dynamic library, 2
instance variables, 27
lookup, 2
memory-management methods, 2
reference counting, 3
selectors, 213
structures, 2
updating, 2
zombie objects, 179
S
scalar types, 30
scheduledTimerWithTimeInterval: method, 258
sectionInfo object, 142
sections property, 142
selectors, 57
calling directly, 213
messages, 50
parameters, 215
runtime, 213
storing to perform after event, 214
completion-handler block referencing, 205
objects synchronized against, 208
serial synchronization queue, 209–213
setFirstName: method, 138
setIdentifier: method, 92
setLastName: method, 138
set prefix, 56
sets, 243
block-based enumeration, 241
objects, 237
setter accessor, 150
setter=<name> attribute, 31
setters
Automatic Reference Counting (ARC), 159
barrier block, 211
naming, 31
properties, 30
scalar types, 30
serial synchronization queue, 210
subclasses overriding, 35
synchronous dispatch to asynchronous dispatch, 210
setup method, 158
setValue:forKey: method, 92
shallow equality, 40
shape class, 7
sharedInstance method, 225–226
Singleton design pattern, 225
singleton objects, 185
Smalltalk, 1
social networking facilities, 235–236
sockets, 162
connections and enumerations, 17–18
someBlock variable, 188
SomeClass class, 68
SomeCPPClass.h header file, 136
someObject receiver, 52
someString variable, 2
stack
autorelease pools, 175
copying blocks to heap, 192
memory allocated for blocks, 192
memory allocated for objects, 2, 3
standard view, 48
start method, 195
states and enumerations, 17–24
static analyzer, 153
static global variables, 48
static library, 233
static qualifier, 14
status codes and enumerations, 17–24
stop variable, 241
storage policies, 47
string literal, 8
string method, 98
strings
characters within range of, 99
converting to lowercase, 98
custom classes, 12
equality, 99
factory method, 98
immutable variants, 12
prefixed by another string, 99
processing, 234
returning hash of, 39
stringValue method, 151
stringWithString: method, 98
strong attribute, 30
__strong qualifier, 159
structs, 53
structures, 2
delegates, 122
instance variables, 122
parameters pointers to, 245
pointers, 191
querying, 122
styles and enumeration types, 22–23
subclasses, 46
overriding setter, 35
Subnet Calc app, xii
superclass
designated initializers, 80
header files defining, 7
inheritance, 7
instance variables, 35
prefixing names, 101
sending messages to, 53
subclasses overriding methods of, 46
super_class variable, 68
supportedInterfaceOrientations method, 20
sychronization locks versus dispatch queues, 208–213
synchronization
Grand Central Dispatch (GCD), 217
synchronization blocks, 208
@synchronized approach, 226
synchronizedMethod method, 208
synchronous dispatch, 210
@synthesize syntax, 28
system frameworks
AVFoundation framework, 235
CFNetwork framework, 234
class clusters, 45
CoreData framework, 235
CoreFoundation framework, 234
CoreText framework, 235
dynamic libraries, 233
Foundation framework, 233, 234
UI frameworks, 235
system libraries
catching and handling exceptions, 165
C++ in, 137
system resources
deterministic life cycles, 163
exceptions thrown, 105
file descriptors, 162
memory, 162
performing tasks on, 223
sockets, 162
system-supplied class, 129
T
table view, 101
tagged pointers, 185
tasks
asynchronously performing, 197
decrementing number of, 220
dispatch groups, 220
priorities, 222
system resources, 223
waiting until finished, 221–222
text, typesetting and rendering, 235
third-party frameworks, 233
third-party libraries
catching and handling exceptions, 165
duplicate-symbol error, 77
threads
implicit autorelease pool, 173
multiple accessing code, 208
priorities, 219
unblocking when long-running tasks occur, 197
thread-safe single-time code execution, 225–226
timers
retain cycle, 262
retain-cycle with repeating, 259–260
run loops and, 258
scheduling, 258
weak references, 262
title: attribute, 42
toll-free bridging, 55, 234, 243–247
try block exceptions, 166
Twitter API, HTTP requests to, 74
Twitter framework
TW prefix, 74
TWRequest class, 202
TWRequest object, 207
2D rendering data structures and functions, 235
TW prefix, 74
TWRequest object, 207
typed constants versus #define preprocessor directive, 12–17
enumerations, 18
types
alias for another type, 194–196
compiler-dependent, 18
enumerations, 18
literal numbers, 9
methods, 100
runtime, 4
variable name inside, 194
U
UIApplicationDelegate object, 163
UIApplicationDidEnterBackgroundNotification constant, 16
UIApplicationMain function, 174
UIApplication object, 147
UIApplicationWillEnterForegroundNotification constant, 16
UIButton class, 42
UI frameworks, 235
UIInterfaceOrientationMask enumerated type, 20
UIKit framework, 13, 16, 19–20, 42, 78, 82, 235
UIKit.h header file, 5
UIKit iOS UI library, 100
UISwitch class, 31
UITableViewCell object, 78
UITableView class, 101
UITableViewController class, 101
UITableViewDelegate protocol, 101
UI thread, 187
UIVewController class, 103–104
UIViewAutoresizing enumeration, 20
UIViewAutoresizingFlexibleHeight option, 19
UIViewAutoresizingFlexibleWidth option, 19
UIViewController class, 5, 100
UIView subclass, 13
unsafe_unretained attribute, 30, 117, 169–171
__unsafe_unretained qualifier, 159
uppercaseString selector, 63, 64–65
urlEncodedString method, 128–129
URLs
downloading data from, 234
errors from parsing or obtaining data from, 106
obtaining data from, 126
retrieved as cache key, 250
user-developed equality methods, 39
userInfo parameter, 262
user interface
unresponsive while task occurs, 197
views, 100
V
value property, 215
values
retain and release callbacks, 247
tagged pointers, 185
variables
_block qualifier, 189
Class type, 68
const qualifier, 14
declaring, 2
forward declaring types for enumerations, 18
holding memory address, 67
isa pointer, 68
local, 14
memory-management semantics, 158–161
static qualifier, 14
strong references to objects, 158
video playback and recording, 235
virtual tables, 2
void return type, 215
void* variable, 191
W
weak references and timers, 262
WebKit web browser framework, 137
web services, 90
while loop, 184
width instance variable, 96
_width instance variable, 80, 84
X
Xcode and zombie objects, 178
Z
zombie classes, 180
zombie objects
debugging memory-management problems, 177–183
detecting, 182
determining original class from, 181
exceptions, 178
turning objects into, 179
zones, segmenting memory into, 109