Re: KVC and KVO for arrays
Re: KVC and KVO for arrays
- Subject: Re: KVC and KVO for arrays
- From: Hans van der Meer <email@hidden>
- Date: Fri, 15 Feb 2008 14:44:36 +0100
When I started this chain the problem was something like:
@interface ... { IBOutlet NSMutableArray *myArray; }
@property(readonly) IBOutlet NSMutableArray *myArray; // ignore
warning on assign
@implementation
@synthesize myArray;
in the init method: myArray = [NSMutableArray arrayWithCapacity:20];
in an action method: [myArray addObject:anObject];
myArray with IB bound through an NSArrayController to a tableview in
the GUI.
I then observed that the addObject did not lead to changes in the GUI:
the KVO mechanism didn't activate.
My thanks to the many people who participated in the discussion. I
followed them with interest and it stimulated me to keep looking into
this problem from other angles.
Finally some light in the tunnel. Reading the documentation on KVC-KVO
may sometimes be tedious, it can be rewarding. After delving in
indexed accessors and mutableArrayValueForKey, I stumbled at last on
manually forcing KVO. It is in the Key-Value Observing Programming
Guide (my version dated 2006-06-28) where on page 15 is said: "Using
automatic observer notifications, it is not necessary to bracket
changes to a property with invocations of willChangeValueForKey: and
didChangeValueForKey:.
Thus where arrays changed in contents did not give an automatic
observer notification one could try these.
So I then coded in the action method:
[self willChangeValueForKey:@"myArray"];
[myArray removeAllObjects];
for (...) { ... [myArray addObject:anObject]; ...}
[self didChangeValueForKey:@"myArray"];
And now the GUI updates! Eureka!
Hans van der Meer
_______________________________________________
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