无法让Cocoa通知工作

Nic*_*boe 0 cocoa objective-c

设置观察者代码:

NSNotificationCenter *defaultCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[defaultCenter addObserver:self
selector:@selector(updateLog:)
    name:@"Update Log"
  object:nil];
Run Code Online (Sandbox Code Playgroud)

发送通知代码:

[[NSNotificationCenter defaultCenter] postNotificationName:@"Update Log" object:self];
Run Code Online (Sandbox Code Playgroud)

使用定义为的方法:

-(void)updateLog: (NSNotification *) notification {
NSLog(@"Update Log"); }
Run Code Online (Sandbox Code Playgroud)

发送通知时,"更新日志"文本不会出现在日志中.感谢您提供有关此代码无效的任何想法.

Geo*_*che 5

"工作区通知通知中心" Apple之间存在差异:

[[NSWorkspace sharedWorkspace] notificationCenter]
Run Code Online (Sandbox Code Playgroud)

"流程的默认通知中心" Apple:

[NSNotificationCenter defaultCenter]
Run Code Online (Sandbox Code Playgroud)

您需要选择其中一个使用.