Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created June 20, 2010 23:46
Show Gist options
  • Save rjungemann/446231 to your computer and use it in GitHub Desktop.
Save rjungemann/446231 to your computer and use it in GitHub Desktop.
Simplest possible event notification system in Cocoa
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSNotificationCenter defaultCenter]
addObserverForName:@"randomEvent" object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSString *message = [[note userInfo] valueForKey:@"message"];
NSLog(@"Received message = %@", message);
}
];
NSDictionary *dict = [[NSDictionary alloc]
initWithObjectsAndKeys: @"Hello, world!", @"message",
nil
];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"randomEvent" object:nil userInfo:
];
[pool drain];
return 0;
}
/*
// You could also setup an observer using an instance method instead of a
// block, like this:
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(handleNotification:)
name:@"randomEvent" object:nil userInfo:nil
];
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment