Cocoa NSNotificationCenter 应用程序之间通信失败

发布于 2024-12-07 05:26:11 字数 1606 浏览 0 评论 0原文

我需要在两个不同的控制台应用程序(观察者和客户端)之间进行通信。

在观察者应用程序中,我添加了以下代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];

在客户端应用程序中,我添加了以下代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];

-(void)trackNotification:(NSNotification*)notif
{
    NSLog(@"trackNotification: %@", notif);
    NSLog(@"trackNotification name: %@", [notif name]);
    NSLog(@"trackNotification object: %@", [notif object]);
    NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}

但没有任何反应。我阅读了所有文档,但我对 Mac OS X 是全新的。 有什么想法吗?


我阅读了有关分布式通知的信息,我更改了代码,现在看起来像这样: 在服务器端:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];

-(void)trackNotification:(NSNotification*)notif
{
    NSLog(@"trackNotification: %@", notif);
    NSLog(@"trackNotification name: %@", [notif name]);
    NSLog(@"trackNotification object: %@", [notif object]);
    NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}

在客户端:

NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
                                   object:nil
                                 userInfo:info 
                       deliverImmediately:YES];

我运行两个应用程序,但没有任何反应。我做错了什么?

I need to communicate between two different console apps, Observer and Client.

In the Observer app I added this code:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];

In the Client app I added this code:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];

-(void)trackNotification:(NSNotification*)notif
{
    NSLog(@"trackNotification: %@", notif);
    NSLog(@"trackNotification name: %@", [notif name]);
    NSLog(@"trackNotification object: %@", [notif object]);
    NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}

but nothing happens. I read all the documentation, but I am brand new in Mac OS X.
Any ideas?


I read about the Distributed Notifications, I changed my code and now looks like this:
In the server side:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"MyNotification" object:nil];

-(void)trackNotification:(NSNotification*)notif
{
    NSLog(@"trackNotification: %@", notif);
    NSLog(@"trackNotification name: %@", [notif name]);
    NSLog(@"trackNotification object: %@", [notif object]);
    NSLog(@"trackNotification userInfo: %@", [notif userInfo]);
}

In the client side:

NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
                                   object:nil
                                 userInfo:info 
                       deliverImmediately:YES];

I run both applications, but nothing happens. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘亓 2024-12-14 05:26:11

NSNotificationCenter 仅适用于一个应用程序内的通知。您想要 NSDistributedNotificationCenter。您可以在通知编程主题中找到更多详细信息。

NSNotificationCenter is only for notifications within one app. You want NSDistributedNotificationCenter. You can find more details in the Notification Programming Topics.

奢望 2024-12-14 05:26:11

我解决了我的问题。这是源代码:
在客户端:

NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys: @"John Doe", @"name", @"22222222222", @"phone", @"Dummy address", @"address", nil];

//Post the notification
NSString *observedObject = @"com.test.net";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center postNotificationName: @"Plugin Notification" object: observedObject userInfo: user deliverImmediately: YES];

在服务器端

NSString *observedObject = @"com.test.net";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self selector: @selector(receiveNotification:) name: @"Plugin Notification" object: observedObject];

receiveNotification 定义

-(void)receiveNotification:(NSNotification*)notif
{
    NSlog(@"Hello");
}

在 dealloc 方法中

[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name: @"Plugin Notification" object: nil];

I solved my problem. This is the source code:
In the client side:

NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys: @"John Doe", @"name", @"22222222222", @"phone", @"Dummy address", @"address", nil];

//Post the notification
NSString *observedObject = @"com.test.net";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center postNotificationName: @"Plugin Notification" object: observedObject userInfo: user deliverImmediately: YES];

In the server side

NSString *observedObject = @"com.test.net";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self selector: @selector(receiveNotification:) name: @"Plugin Notification" object: observedObject];

receiveNotification definition

-(void)receiveNotification:(NSNotification*)notif
{
    NSlog(@"Hello");
}

In dealloc method

[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name: @"Plugin Notification" object: nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文