Observing Yourself in Cocoa

In Cocoa, addObserver:forKeyPath:options:context:"neither the receiver nor anObserver" is saved. Therefore, I assume that self-observation is permitted; that is, it is absolutely true to do something like

[self addObserver:self forKeyPath...]

Until you forget to unregister selfas an observer as the first thing to do in dealloc.

Is this assumption correct?

+5
source share
3 answers

Yes, in fact there is no reason why you cannot observe yourself. But, as you said, like any KVO observation, be sure to remove yourself as an observer before you free yourself.

, , - , . , . KVO , , .

+12

, . :

//.h
...
@property(readwrite, retain, setter=setMyPropertySynth:) id myProperty;
-(void)setMyProperty:(id)newValue;
....


//.m
...
@synthesize myProperty;

-(void)setMyProperty:(id)newValue
{
    //add code here

    [self setMyPropertySynth:newValue];

    //add more code here
}
...
+1

-dealloc. ? , , ; -dealloc . -dealloc -finalize , .

-1

All Articles