Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 应用程序之间的通知中心通信失败_Cocoa_Nsnotifications_Nsnotificationcenter - Fatal编程技术网

Cocoa 应用程序之间的通知中心通信失败

Cocoa 应用程序之间的通知中心通信失败,cocoa,nsnotifications,nsnotificationcenter,Cocoa,Nsnotifications,Nsnotificationcenter,我需要在两个不同的控制台应用程序,观察者和客户端之间进行通信 在Observer应用程序中,我添加了以下代码: [[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(trackNotification:) name:@"M

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

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

[[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]);
}
在客户端应用程序中,我添加了以下代码:

[[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]);
}
但什么也没发生。我阅读了所有的文档,但我对MacOSX是全新的。 有什么想法吗


我阅读了有关分布式通知的内容,更改了代码,现在看起来如下所示: 在服务器端:

[[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];
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];

我运行这两个应用程序,但什么也没发生。我做错了什么?

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

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

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

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
                                   object:nil
                                 userInfo:info 
                       deliverImmediately:YES];
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];
接收通知定义

-(void)receiveNotification:(NSNotification*)notif
{
    NSlog(@"Hello");
}
在dealoc方法中

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

谢谢我要去看看。