KVO on a key path with nil intermediate properties
KVO on a key path with nil intermediate properties
- Subject: KVO on a key path with nil intermediate properties
- From: Rick Mann <email@hidden>
- Date: Wed, 26 Jun 2013 18:51:34 -0700
I've looked through the docs and googled, but I can't find an answer to this question, so if it's obvious, forgive me.
Can one set up KVO on a property of a property if the intermediate one is nil, and then get notified when either changes? In practice, it seems not, as I don't get notified for either the intermediate property or the leaf property. In that case, it seems that I have to observer the intermediate properties individually, and then when those change, ignore and observe their sub properties.
Let's say I have three classes, A, B, and C. A has a b* b property, and B and C have properties foo and bar, respectively:
@interface B : NSObject
@property (strong) NString* bar;
@end
---
@interface A : NSObject
@property (strong) B* foo;
@end
---
Can I do:
@implementation SomeClass
- (void)
observeValueForKeyPath: (NSString*) inKeyPath
ofObject: (id) inObject
change: (NSDictionary*) inChange
context: (void*) inContext
{
if ([inKeyPath isEqualToString: @"foo.bar"])
{
// Yay!
// Notified when a.foo changes
// Notified when a.foo.bar changes
}
else
{
[super observeValueForKeyPath: inKeyPath ofObject: inObject change: inChange context: inContext];
}
}
- (void)
test
{
A* a = [[A alloc] init];
[a addObserver: self
forKeyPath: @"foo.bar"
options: NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context: NULL];
...
And be notified in these two instances:
...
B* b = [[B alloc] init];
a.foo = b; // Notified because foo changed from nil to something
b.bar = @"something"; // Notified because bar changed
}
--
Rick
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden