1

メモリ不足の警告に応答できるようにしたい「静的」のようなクラスがあります。ただし、シミュレーターからメモリ不足の警告を手動でトリガーすると、「認識されないセレクター」エラーが発生します。

関連コード:

@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
4

1 に答える 1

2

メソッド名はreceiveNotification:(コロンが名前の一部であることに注意してください)

したがって、セレクターは@selector(receiveNotification:)

編集:また、ところで、クラス初期化子で[スーパー初期化]を呼び出しません。同様に、作成したこの初期化子が 2 回呼び出される原因となるサブクラスを防ぐ必要があります。詳細については、Mike Ash の非常に優れた投稿を参照してください:クラスの読み込みと初期化

それが役立つことを願っています。

于 2012-06-10T02:52:54.727 に答える