Bindings clarification: +setKeys:triggerChangeNotificationsForDependentKey: doesn't affect subclasses?
Bindings clarification: +setKeys:triggerChangeNotificationsForDependentKey: doesn't affect subclasses?
- Subject: Bindings clarification: +setKeys:triggerChangeNotificationsForDependentKey: doesn't affect subclasses?
- From: Jim Correia <email@hidden>
- Date: Thu, 28 Jul 2005 16:09:09 -0400
The documentation for this method is fairly short:
setKeys:triggerChangeNotificationsForDependentKey:
+ (void)setKeys:(NSArray *)keys
triggerChangeNotificationsForDependentKey:(NSString *)dependentKey
Register the fact that invocations of
willChangeValueForKey:/didChangeValueForKey: and
willChange:valuesAtIndexes:forKey:/didChange:
valuesAtIndexes:forKey: for
any key in keys should automatically invoke the same methods for
the
dependentKey.
It doesn't say whether the same message needs to be sent to the class
object of every subclass. A short sample program shows that this is
necessary (included below.)
Is it necessary to send this message to the class object for every
subclass? (If so, it is rather unfortunate as it means subclasses
have to know about the implementation details of their super. Covered
in a somewhat more verbose form in yesterday's question about
registration and +initialize.)
If this is the case, I'll happily write a documentation bug. If not,
I'll write a bug against NSKeyValueObserving.
Thanks,
Jim
===
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSMutableDictionary *properties;
}
- (NSString *)name;
- (void)setName:(NSString *)name;
- (NSString *)uppercaseName;
@end
@implementation Person
- (id)init
{
self = [super init];
if (self != nil)
properties = [[NSMutableDictionary alloc] init];
return self;
}
- (NSString *)name
{
return [properties objectForKey: @"name"];
}
- (void)setName:(NSString *)name
{
[properties setObject: name forKey: @"name"];
}
- (NSString *)uppercaseName
{
return [[self name] uppercaseString];
}
@end
@interface Employee : Person
{
}
@end
@implementation Employee
@end
@interface Watcher : NSObject
{
}
- (void)watchPerson:(Person *)person;
@end
@implementation Watcher
- (void)watchPerson:(Person *)person
{
[person addObserver: self forKeyPath: @"name" options: 0
context: NULL];
[person addObserver: self forKeyPath: @"uppercaseName" options:
0 context: NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"keyPath \"%@\" changed for %@", keyPath, object);
}
@end
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[Person setKeys: [NSArray arrayWithObject: @"name"]
triggerChangeNotificationsForDependentKey: @"uppercaseName"];
Person *person = [[Person alloc] init];
Employee *employee = [[Employee alloc] init];
Watcher *watcher = [[Watcher alloc] init];
[watcher watchPerson: person];
[watcher watchPerson: employee];
[person setName: @"Fred"];
[employee setName: @"Barney"];
// leak instances - sample app only
[pool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden