Search code examples
cocoaselectornsnotificationcenter

NotificationCenter : -[NSRunLoop ]: unrecognized selector sent to instance


i'm new to Cocoa and I'm having a little trouble with a sample app i'm writing :

@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

With that piece of code i'm getting a the following error when I plug-in a USB drive :

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

Any reason why ?

Thx


Solution

  • You need to define dealloc method of DeviceDetection-

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

    EDIT 1 -

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

    Above error occurs because you haven't defined value in DeviceDetection class.