You can observe value changes for bar
using KVO from Foo like this:
[bar addObserver:self
forKeyPath:@"intVal"
options:NSKeyValueObservingOptionNew
context:nil];
[bar addObserver:self
forKeyPath:@"longVal"
options:NSKeyValueObservingOptionNew
context:nil];
And then you need to implement observeValueForKey
method like following:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([keyPath isEqualToString:@"longVal"]) {
// property longVal changed
}
...
}
EDIT:
Something that has been pointed out is the correctness of the answer, so I'll try to make it more complete:
declare a global variable in Foo like this:
static void * const MyClassKVOContext = (void*)&MyClassKVOContext;
and pass it as context when you add the observer:
[bar addObserver:self
forKeyPath:@"longVal"
options:NSKeyValueObservingOptionNew
context:MyClassKVOContext];
To finish, when you observe:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if (context != MyClassKVOContext) {
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context]; return; }
if ([keyPath isEqualToString:@"longVal"]) {
// property longVal changed
}
...
}
This will guarantee the maximum of uniqueness possible for the observation context. For a more complete explanation please refer to the linked answer in the comments, as I still think is out of the scope of this topic.