KVC and KVO for arrays
KVC and KVO for arrays
- Subject: KVC and KVO for arrays
- From: Hans van der Meer <email@hidden>
- Date: Tue, 12 Feb 2008 21:32:56 +0100
I came along this problem because I understand the binding mechanism
requires kvc calls in order to get the observing going. It is unclear
to me how to do this for changes in array. (I did read the KVC and KVO
Programming Guides).
Setting an array works, for example:
NSMutableArray *myArray;
@property(copy) NSMutableArray *myArray;
@synthesize myArray;
....
myArray = [NSMutableArray arrayWithCapacity:10];
// fill myArray with objects for ex. with [myArray addObject:..] etc.
[self setMyArray:myArray]
In the above code the NSArrayController bound to myArray a Model Key
Path gets informed of the change.
In the documentation there is mention of "indexed accessor methods"
that will directly inform the array of changes. For changes these are
given as insertObject:in<Key>AtIndex: and removeObjectFrom<Key>AtIndex:
The <key> in this case being MyArray.
That sounds fine for implementing array accessing in a custom class of
my own. But what for the standard NSArrayMutable array? When coding:
[myArray insertObject:anObject inMyArrayAtIndex:0]
the compiler warns me that NSMutableArray does not know this method
and on executing I get the more or less expected error: [NSCFArray
insertObject:inSolutionsAtIndex:]: unrecognized selector sent to
instance.
Is there something I do not see? Or do I have to add all the "indexed
accessor methods" to NSArray and NSMutableArray in a category?
That generates another question: in that case, must I program for
manual observing by bracketing the implementation statements with
[self willChangeValueForKey ..etc] messages?
Finally I tried:
myArray = [[NSMutableArray arrayWithCapacity:10]
mutableArrayValueForKey:@"myArray"];
[myArray addObject:anObject];
But that did not work either, the NSArrayController not being updated.
Though no NSUndefinedKeyException was raised, so
mutableArrayValueForKey must have had some effect.
Could someone possibly enlighten me? Thanks in advance.
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