Je dois communiquer entre deux applications de console différentes, Observer et Client.Cocoa NSNotificationCenter la communication entre les applications a échoué
Dans l'application Observer j'ai ajouté ce code:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
Dans l'application Client j'ai ajouté ce 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]);
}
, mais rien ne se passe. J'ai lu toute la documentation, mais je suis flambant neuf sous Mac OS X. Des idées?
Je lis sur les notifications distribuées, j'ai changé mon code et ressemble maintenant à ceci: Du côté du serveur:
[[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]);
}
Dans le côté client:
NSMutableDictionary *info;
info = [NSMutableDictionary dictionary];
[info setObject:@"foo" forKey:@"bar"];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"MyNotification"
object:nil
userInfo:info
deliverImmediately:YES];
I exécuter les deux applications, mais rien ne se passe. Qu'est-ce que je fais mal?
Merci! Je vais jeter un coup d'oeil. – Ana