我是可可的新手,我在编写示例应用程序时遇到了一些麻烦:

@implementation DeviceDetection
- (id) init {
    self = [super init];
    if (self) {

        notCenter = [[NSWorkspace sharedWorkspace] notificationCenter];
        [notCenter addObserver:self
                      selector:@selector(discMounted:)
                          name:@"NSWorkspaceDidMountNotification"
                        object:[NSWorkspace sharedWorkspace]]; // Register for all notifications
    }

    return self;
}

- (void)discMounted:(NSNotification *)notification
{
    NSLog(@"COUCOU");
}
@end



#import <Foundation/Foundation.h>

@interface DeviceDetection : NSObject {

    NSNotificationCenter *notCenter;

}

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


@end




@implementation AppDelegate
@synthesize window = _window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    DeviceDetection* d = [[DeviceDetection alloc] init];

    [d value];
}
@end


有了这段代码,当我插入USB驱动器时出现以下错误:

[NSRunLoop discMounted:]: unrecognized selector sent to instance 0x10054c5a0


有什么原因吗?

谢谢

最佳答案

您需要定义dealloc-的DeviceDetection方法

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


编辑1-

DrivesOnDock[5207:707] -[DeviceDetection value]: unrecognized selector sent to instance 0x100475b40 when the app starts.


发生上述错误是因为尚未在value类中定义DeviceDetection

关于cocoa - NotificationCenter:-[NSRunLoop]:无法识别的选择器已发送到实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10640634/

10-12 06:19