during the design of a component it turned out that I would need a “one-shot” handler of some notification. I’d have an object that would respond to a notification only once and then it should delete itself. My object is initialized with autorelease and I can’t change this. Would the following pattern right to achieve this?
- (void) init
{
[[NSNotificationCenter defaultCenter] addObserver:[self retain]
selector:@selector(doRespond)
name:@"someNotification"
object:nil];
}
- (void) doRespond
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"someNotification"
object:nil];
// do something
[self release];
}
That would from first sight be the correct way to do it.