rewriting observed keyPath
rewriting observed keyPath
- Subject: rewriting observed keyPath
- From: Remco Poelstra <email@hidden>
- Date: Tue, 15 Feb 2011 14:11:26 +0100
Hi,
I've a "PresetsController" which holds a dictionary containing preset settings for my application. The presets contain trees (Mutable Dictionaries) of keys.
To save the GUI code from bothering with tracking the current preset, I want to give my PresetController the option to replace a keyPath like @"current.parameter.subparameter.value" to @"preset2.parameter.subparameter.value".
I implemented it with a valueForUndefinedKey:
- (id) valueForUndefinedKey:(NSString *)key {
if ([key isEqual:@"current"])
return [presets valueForKey:currentPreset]; //presets is a NSMutableDicitonary, currentPreset a NSString
else
return [presets valueForKey:key];
}
, returning the dictionary belonging to the current preset. This works for setting and reading using keyPaths. It does not work for observing a keyPath like @"current.parameter.subparameter.value". How should I implement that? I tried monitoring all keyPaths in the dictionaries, and than calling [self will/didChange] like:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSString *newKeyPath=[keyPath stringByReplacingOccurrencesOfString:currentPreset withString:@"current"];
if ([keyPath hasPrefix:currentPreset]) {
if ([[change valueForKey:NSKeyValueChangeNotificationIsPriorKey] boolValue])
[self willChangeValueForKey:newKeyPath];
else
[self didChangeValueForKey:newKeyPath];
}
}
but this doesn't trigger anything. If I replace newKeyPath with @"current" than all observers will get a trigger, independent of any (sub)parameters.
What would be the correct way to trigger the observers?
Kind regards,
Remco Poelstra
_______________________________________________
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