DuckMaestro
DuckMaestro

Reputation: 15885

"unrecognized selector" when trying to listen for UIApplicationDidReceiveMemoryWarningNotification

I have a "static"-like class that I want to be able to respond to low memory warnings. However when I trigger the low memory warning manually from the simulator I'm receiving an "unrecognized selector" error.

Relevant code:

@interface MyClass : NSObject
+ (void) receiveNotification:(NSNotification*) notification;
@end 

@implementation MyClass
+ (void) initialize {
    [super initialize];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification) name:@"UIApplicationDidReceiveMemoryWarningNotification" object:nil];
}
+ (void) receiveNotification:(NSNotification*) notification {
    // Breakpoint here never hits.
    // I instead receive error "+[MyClass receiveNotification]: unrecognized selector sent to class".
}
@end

Upvotes: 1

Views: 493

Answers (1)

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

Your method name is receiveNotification: (note the colon is part of the name)

So the selector should be @selector(receiveNotification:)

EDIT: also, btw, I wouldn't call [super initialize] in a class initializer. Similarly, you should guard against a subclass causing this initializer you wrote to be invoked twice. See this very nice post from Mike Ash for more on this: class loading and initialization

I hope that helps.

Upvotes: 2

Related Questions