Snow Leopard 中 NSWorkspace 和 NSNotificationCentre 基于块的 API 的问题

发布于 2024-08-11 04:52:58 字数 1138 浏览 7 评论 0原文

我在使用 Snow Leopard 的新的基于块的 API 来观察来自 NSWorkspace 的 NSNotificationCenter 的通知时遇到了一些问题。

如果我使用传统的基于选择器的方法注册通知,那么我就能够观察到所需的通知。如果我尝试使用需要块的新方法,那么它不起作用。

在下面的代码块中,将 usingBlockNotifications 设置为 YES 或 NO 应该会产生相同的结果,即应用程序启动时“didReceiveNoticationTest: Called”打印到控制台,但当它设置为 YES 时我没有收到消息。

关于我做错了什么有什么建议吗?

-(void)awakeFromNib

{

 BOOL usingBlockNotifications = YES;

 _notifcationObserver = nil;
 NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter];

 if (usingBlockNotifications)
 { 
  _notifcationObserver = 
  [nc addObserverForName:NSWorkspaceDidLaunchApplicationNotification
      object:[NSWorkspace sharedWorkspace]
       queue:nil
     usingBlock:^(NSNotification *arg1) {
      [self didReceiveNoticationTest:arg1];
     }];
  [_notifcationObserver retain];
 } else {
  [nc addObserver:self
      selector:@selector(didReceiveNoticationTest:)
       name:NSWorkspaceDidLaunchApplicationNotification
     object:[NSWorkspace sharedWorkspace]];
 }

}

-(void)didReceiveNoticationTest:(NSNotification *)notification
{
 NSLog(@"didReceiveNoticationTest: called");
}

I've been having some trouble with Snow Leopard's new block-based API for observing notifications from NSWorkspace's NSNotificationCenter.

If I register for notifications using the traditional selector-based method, then I am able to observe the desired notification. If I try using the new method that takes a block, then it doesn't work.

In the code block below, setting usingBlockNotifications to either YES or NO should produce the same result, i.e. "didReceiveNoticationTest: called" printed to the console when an app launches, but I don't get the message when it's set to YES.

Any suggestions on what I'm doing wrong?

-(void)awakeFromNib

{

 BOOL usingBlockNotifications = YES;

 _notifcationObserver = nil;
 NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter];

 if (usingBlockNotifications)
 { 
  _notifcationObserver = 
  [nc addObserverForName:NSWorkspaceDidLaunchApplicationNotification
      object:[NSWorkspace sharedWorkspace]
       queue:nil
     usingBlock:^(NSNotification *arg1) {
      [self didReceiveNoticationTest:arg1];
     }];
  [_notifcationObserver retain];
 } else {
  [nc addObserver:self
      selector:@selector(didReceiveNoticationTest:)
       name:NSWorkspaceDidLaunchApplicationNotification
     object:[NSWorkspace sharedWorkspace]];
 }

}

-(void)didReceiveNoticationTest:(NSNotification *)notification
{
 NSLog(@"didReceiveNoticationTest: called");
}

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

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

发布评论

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

评论(2

倾城月光淡如水﹏ 2024-08-18 04:52:58

我敢说这是一个错误。

以下代码打印这两个通知。如果删除选择器版本,则不会打印任何内容。如果删除块版本,选择器版本仍会打印。

-(void)didReceiveNoticationTest1:(NSNotification *)notification
{
    NSLog(@"1: didReceiveNoticationTest: called");
}
-(void)didReceiveNoticationTest2:(NSNotification *)notification
{
    NSLog(@"2: didReceiveNoticationTest: called");
}

-(void)awakeFromNib

{
    NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter];

    [nc addObserver:self
           selector:@selector(didReceiveNoticationTest1:)
               name:NSWorkspaceDidLaunchApplicationNotification
             object:[NSWorkspace sharedWorkspace]];

    [[nc addObserverForName:NSWorkspaceDidLaunchApplicationNotification
                     object:[NSWorkspace sharedWorkspace]
                      queue:nil
                 usingBlock:^(NSNotification* arg1)
                            {
                                [self didReceiveNoticationTest2:arg1];
                            }] retain]; // this will leak.
}

I daresay this is a bug.

The following code prints both notifications. If you remove the selector version, nothing is printed. If you remove the block version the selector version still prints.

-(void)didReceiveNoticationTest1:(NSNotification *)notification
{
    NSLog(@"1: didReceiveNoticationTest: called");
}
-(void)didReceiveNoticationTest2:(NSNotification *)notification
{
    NSLog(@"2: didReceiveNoticationTest: called");
}

-(void)awakeFromNib

{
    NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter];

    [nc addObserver:self
           selector:@selector(didReceiveNoticationTest1:)
               name:NSWorkspaceDidLaunchApplicationNotification
             object:[NSWorkspace sharedWorkspace]];

    [[nc addObserverForName:NSWorkspaceDidLaunchApplicationNotification
                     object:[NSWorkspace sharedWorkspace]
                      queue:nil
                 usingBlock:^(NSNotification* arg1)
                            {
                                [self didReceiveNoticationTest2:arg1];
                            }] retain]; // this will leak.
}
む无字情书 2024-08-18 04:52:58

重新阅读该方法的文档,特别是以下部分:

返回值

一个对象或符合
到 NSObject 协议。

必须保留返回的值
只要你想注册
存在于通知中心。

Re-read the documentation for that method, specifically the part:

Return Value

An object or conforming
to the NSObject protocol.

You must retain the returned value as
long as you want the registration to
exist in the notification center.

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