Snow Leopard 中 NSWorkspace 和 NSNotificationCentre 基于块的 API 的问题
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我敢说这是一个错误。
以下代码打印这两个通知。如果删除选择器版本,则不会打印任何内容。如果删除块版本,选择器版本仍会打印。
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.
重新阅读该方法的文档,特别是以下部分:
Re-read the documentation for that method, specifically the part: