Created
June 20, 2010 23:46
-
-
Save rjungemann/446231 to your computer and use it in GitHub Desktop.
Simplest possible event notification system in Cocoa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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