gpt4 book ai didi

objective-c - Snow Leopard 中 NSWorkspace 和 NSNotificationCentre 基于 block 的 API 的问题

转载 作者:行者123 更新时间:2023-12-03 17:00:29 35 4
gpt4 key购买 nike

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

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

在下面的代码块中,将 usingBlockNotifications 设置为 YES 或 NO 应该会产生相同的结果,即应用程序启动时,“didReceiveNotificationTest: 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");
}

最佳答案

我敢说这是一个错误。

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

-(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.
}

关于objective-c - Snow Leopard 中 NSWorkspace 和 NSNotificationCentre 基于 block 的 API 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1726008/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com