Still struggling with the nuts and bolts of KVC. I understood eventually that just observing a property (test) which was an NSMutableArray and then mutating it with removeObjectAtIndex: isn't KVC compliant, didn't give the observers callbacks, and implemented the removeObjectFromTestAtIndex: method. Then I finally figured out that I can either call that method directly, or I can get mutableArrayValueForKey:@"test" and use the normal array operations on that proxy as it's KVC compliant.

However I failed to make it work until I moved the getter, mutableArrayValueForKey:@"test" *after* the line I where set up my KVO observation. If I get this mutable array beforehand, I don't get the notification. Why does it matter when I get the mutableArrayValueForKey:?

As the code is set up below, I won't get a KVO callback for [ test2 removeObjectAtIndex:0 ], if however you comment out the 'place one' line and uncomment the 'place two' line, you will get the callback. In both cases [ test2 removeObjectAtIndex:0 ] does call the removeObjectFromTestAtIndex: method, so I would expect notificiation.

Bar *bar = [ [ Bar alloc ] init ]; // bar has one property, test, an NSMutableArray and it has the KVC mutator methods defined. Baz *baz = [ [ Baz alloc ] init ]; // baz has nothing but a logging observeValueForKeyPath: method

        
NSMutableArray *test2 = [ bar mutableArrayValueForKey:@"test" ]; // place one

        [ bar addObserver:baz forKeyPath:@"test" options:0 context:nil ];

//NSMutableArray *test2 = [ bar mutableArrayValueForKey:@"test" ]; // place two
        
// only get a callback on this one if I get the mutableArrayValueForKey: in place two, after I've added the observer.
        [ test2 removeObjectAtIndex:0 ];
        
        // always get a KVO callback for this one
        [ bar removeObjectFromTestAtIndex:0 ];
        
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to