01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Summary<br />

199<br />

addObserver:selector:name:object: provides a lot of flexibility<br />

The addObserver:selector:name:object: message provides a lot of flexibility<br />

when it comes to deciding how notifications are handled by an observer.<br />

Since it’s possible to call addObserver:selector:name:object: multiple times for<br />

the same observer object, with a different name argument each time, it’s possible for<br />

a single object to process more than one type of notification.<br />

Likewise, via use of the selector argument, you can determine if different notification<br />

types are handled by separate methods or lumped together and processed by a<br />

single method.<br />

If you decide a single method can process multiple types of notifications, the NSNotification<br />

object passed into the method has a name property, which can help distinguish<br />

why it is currently invoked.<br />

receive notification events. This can be done by sending a removeObserver:name:<br />

object:, as demonstrated here:<br />

- (void)dealloc {<br />

[[NSNotificationCenter defaultCenter]<br />

removeObserver:self<br />

name:UIApplicationDidReceiveMemoryWarningNotification<br />

object:nil];<br />

}<br />

[address release];<br />

[super dealloc];<br />

It’s important to unregister your observers before your object is deallocated. Otherwise,<br />

when the notification is next generated, the NSNotificationCenter will attempt<br />

to send a message to the nonexistent object and your application may crash.<br />

9.7 Summary<br />

Responsibility for correct memory management in <strong>Objective</strong>-C rests significantly with<br />

you, the developer. Unlike in garbage-collected environments, which tend to be useand-forget<br />

type environments, the deallocation of objects in <strong>Objective</strong>-C will occur at<br />

the correct point in time only through the proper use of the retain, release, and<br />

autorelease messages. Make a mistake, and the object will never be deallocated or,<br />

worse yet, it may be deallocated too early and lead to application crashes.<br />

To make things easier, most <strong>Objective</strong>-C classes strive to stick to a common memory<br />

management pattern, which was discussed in section 9.5. By handling memory<br />

allocation in a consistent manner, you won’t need to constantly refer to documentation<br />

in order to determine who’s responsible for keeping track of a given object.<br />

An autorelease pool enables some of the benefits of a garbage-collected environment<br />

without the implied overhead. An object can be registered to be sent a release<br />

message at some point in the future. This means you don’t use the traditional retain

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!