objective-c – NSWorkspacedidmountNotify /可能是通用的NSWorkspace

我是一名经验丰富的C/C++程序员,但却是ObjC的新成员.我正试图在Mac OSX项目中捕获NSWorkspacedidmountnofification.

我将回调添加到我的app delegate界面.

- (void)mediaMounted:(NSNotification *)notification;

实施包括

- (void)mediaMounted:(NSNotification *)aNotification {
    NSLog(@"mediaMounted volume change.");
}

在我的applicationDidFinishLaunching中,我将自己添加到通知中心.

NSNotificationCenter* ncenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[ncenter addObserver: self
            selector: @selector(mediaMounted)
                name: NSWorkspaceDidMountNotification
              object: nil];    

但是,当我运行并装载磁盘时,我看到:

 2012-08-29 09:52:31.753 OSN[2203:903] -[OSNAppDelegate mediaMounted]: unrecognized selector sent to instance 0x101340af0
 2012-08-29 09:52:31.756 OSN[2203:903] -[OSNAppDelegate mediaMounted]: unrecognized selector sent to instance 0x101340af0

我确认Instance 0x101340af0是我的OSNAppDelegate self,但我不明白我还需要做什么,以便识别选择器.

最佳答案 你的选择器应该是:

mediaMounted:不是mediaMounted.

您的实现将NSNotification作为参数,而不是任何内容.

您可以通过以下方式验证选择

if( [self respondsToSelector:@selector(mediaMounted)] )
{
  NSLog(@"Good to go");
}

例:

// this selector
@selector(test)

// will call this method
- (void)test{ }

// but this selector, noting the :
@selector(test:)

// would call this method
- (void)test:(id)sender{ }

您可以阅读有关选择器here的信息.

点赞