I have trouble with KeyValueCoding and willChange
Description
In your code, you are doing something like this
@interface Bar : NSObject
{
NSMutableArray *collection;
}
@end
Bar *bar; [bar addObject:foo toPropertyWithKey:@"collection"];
but the change does not appear in the user interface.
How to fix it
Conceptually one may expect, that willChange would be called automatically for you, but this is not the case. To fix this either call willChange like this beforehand
[bar willChange]; [bar addObject:foo toPropertyWithKey:@"collection"];
or add an accessor to Bar
-(void) addToCollection:(Foo *) foo
{
[self willChange];
[collection addObject:foo];
}
Why things are as they are
The reason why addObject:toPropertyWithKey: and other related KVC messages, like takeValue:forKey: do not call willChange for you is actually fairly involved. It goes something like this:
Currently MulleEOF uses the KVC mechanism of Mac OS X Foundation. Foundation does not use willChange but uses KeyValueObserving instead. KVO is not used by MulleEOF, because KeyValueObserving (AFAIK) uses different semantics (post-change instead of pre-change notification). Also it's not portable.
Since KeyValueCoding is fundamental to Foundation, it likely would be a bad idea to replace these methods with some MulleEOF implementations of the same name.
The other idea would be to create a different set of KVC methods, but this doesn't help existing code and the confusion introduced by the duplication of KVC mechanisms does not sound very attractive.
So willChange is not directly supported by KeyValueCoding. It is indirectly supported by it, if you take care to write accessors for your instance variables.