1996-03-03 02:18:46 +00:00
|
|
|
/* The simplest of tests for the NSNotification and NSNotificationCenter
|
|
|
|
classes. These tests should be expanded.
|
|
|
|
|
|
|
|
(The Tcp*Port classes, however, do test the notification mechanism
|
|
|
|
further.) */
|
|
|
|
|
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
#include <Foundation/NSString.h>
|
1996-03-07 00:39:51 +00:00
|
|
|
#include <Foundation/NSAutoreleasePool.h>
|
1996-03-03 02:18:46 +00:00
|
|
|
|
|
|
|
@interface Observer : NSObject
|
|
|
|
- (void) gotNotificationFoo: not;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation Observer
|
|
|
|
|
|
|
|
- (void) gotNotificationFoo: (NSNotification*)not
|
|
|
|
{
|
1999-02-16 16:29:07 +00:00
|
|
|
printf ("Got %s\n", [[not name] cString]);
|
1996-03-03 02:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) gotNotificationFooNoObject: (NSNotification*)not
|
|
|
|
{
|
1999-02-16 16:29:07 +00:00
|
|
|
printf ("Got %s without object\n", [[not name] cString]);
|
1996-03-03 02:18:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
id foo = @"NotificationTestFoo";
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
id o1 = [NSObject new];
|
|
|
|
id observer1 = [Observer new];
|
1996-03-07 00:39:51 +00:00
|
|
|
id arp;
|
|
|
|
|
|
|
|
arp = [NSAutoreleasePool new];
|
1996-03-03 02:18:46 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFoo:)
|
|
|
|
name: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
addObserver: observer1
|
|
|
|
selector: @selector(gotNotificationFooNoObject:)
|
|
|
|
name: foo
|
|
|
|
object: nil];
|
|
|
|
|
1996-03-07 00:39:51 +00:00
|
|
|
|
1996-03-03 02:18:46 +00:00
|
|
|
/* This will cause two messages to be printed, one for each request above. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
/* This will cause one message to be printed. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: nil];
|
|
|
|
|
1996-03-07 00:39:51 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
removeObserver: observer1
|
|
|
|
name: nil
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
/* This will cause message to be printed. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
removeObserver: observer1];
|
|
|
|
|
|
|
|
/* This will cause no messages to be printed. */
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: foo
|
|
|
|
object: o1];
|
|
|
|
|
|
|
|
[arp release];
|
|
|
|
|
1996-03-03 02:18:46 +00:00
|
|
|
exit (0);
|
|
|
|
}
|