Objective-C: About Notifications

I will be paraphrasing an extract from the excellent objective-C text, Cocoa Design Patterns  by Donald A. Yacktman, which is an excellent book on design patterns for for effective framework usage. In this extract, we will talk about Notifications.

An instance of the NSNotification class encapsulates information to be broadcasted to objects that have registered themselves via an instance of NSNotificationCenter class, often referred to as an observer object



A single notification (NSNotification) may be broadcasted to many observers via the NSNotificationCenter, with the NSNotificationCenter storing a list of registered observers. Notifications may be posted by any number of objects and received by any number of objects (observers).

Difference between Delegates and Notifications
Delegates are synonymous with working with anonymous objects that need to ‘actively influence events as they happen’, whilst Notifications are used when anonymous objects need to passively observe and react to important events. 

An example
Notification class:

@class MYNotification : NSObject {
NSString *name; // Identifies the notification
id object; // an anonymous object
NSDictionary *infoDictionary; // arbitrary associated info

- (id)initWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)someUserInfo;
@property (readonly, copy) NSString *name;
@property (readonly, assign) id object;
@property (readonly, copy) NSDictionary *infoDictionary;
@implementation MYNotification
@synthesize name;
@synthesize object;
@synthesize infoDictionary;

-(id)initWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)someUserInfo
[self setName:aName];
[self setObject:anObject];
[self setInfoDictionary:someUserInfo];
return self;

- (void)dealloc {
[self setName:nil];
style="font-size: 10px;"> [self setObject:nil];
[self setInfoDictionary:nil];
[super dealloc];

NotificationCenter class:
This class stores information about the observers in a container called observersDictionary.MyNotificationCenter:

@class MYNotificationCenter : NSObject {
NSMutableDictionary *observersDictionary;
- (id)defaultCenter;
- (void)addObserver:(id)notificationObserver
name:(NSString *)notificationName

- (void)removeObserver:(id)notificationObserver;
- (void)postNotification:(MYNotification *)aNotification;
- (void)postNotificationName:(NSString *)aName object:(id)objectOfInterest userInfo:(NSDictionary *)someUserInfo;

This was just an extract from the chapter on delegates, from the book Cocoa Design Patterns by Donald A. Yacktman (2009).  The book is at 40% off and with free shipping, definitely worth checking out.
This entry was posted in General and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s