Skip navigation.
 
mlKVC and KVO for arrays
FROM : Hans van der Meer
DATE : Tue Feb 12 21:32:56 2008

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

Related mailsAuthorDate
mlKVC and KVO for arrays Hans van der Meer Feb 12, 21:32
mlRe: KVC and KVO for arrays Hans van der Meer Feb 12, 23:07
mlRe: KVC and KVO for arrays Adam P Jenkins Feb 12, 23:09
mlRe: KVC and KVO for arrays mmalc crawford Feb 13, 14:00
mlRe: KVC and KVO for arrays Adam P Jenkins Feb 13, 15:37
mlRe: KVC and KVO for arrays mmalc crawford Feb 13, 16:34
mlRe: KVC and KVO for arrays Adam P Jenkins Feb 13, 17:51
mlRe: KVC and KVO for arrays Jens Alfke Feb 13, 18:08
mlRe: KVC and KVO for arrays Adam P Jenkins Feb 13, 20:24
mlRe: KVC and KVO for arrays mmalc crawford Feb 13, 22:21
mlRe: KVC and KVO for arrays mmalc crawford Feb 13, 22:30
mlRe: KVC and KVO for arrays Kyle Sluder Feb 13, 22:30
mlRe: KVC and KVO for arrays Adam P Jenkins Feb 13, 22:57
mlRe: KVC and KVO for arrays Ken Thomases Feb 13, 23:05
mlRe: KVC and KVO for arrays Jens Alfke Feb 14, 00:33
mlRe: KVC and KVO for arrays Adam P Jenkins Feb 14, 07:47
mlRe: KVC and KVO for arrays John Dann Feb 14, 15:05
mlRe: KVC and KVO for arrays Nick Toumpelis Feb 15, 10:54
mlRe: KVC and KVO for arrays John Dann Feb 15, 11:27
mlRe: KVC and KVO for arrays Nick Toumpelis Feb 15, 13:20
mlRe: KVC and KVO for arrays Hans van der Meer Feb 15, 14:44
mlRe: KVC and KVO for arrays John Dann Feb 15, 15:10
mlRe: KVC and KVO for arrays Stephen Hoffman Feb 15, 16:18
mlProper use of #define to aid in compile-time checking of strings (was KVC and KVO for arrays) John Dann Feb 15, 16:52
mlRe: KVC and KVO for arrays Kyle Sluder Feb 15, 18:43
mlRe: Proper use of #define to aid in compile-time checking of strings (was KVC and KVO for arrays) Nick Toumpelis Feb 15, 22:23
mlRe: Proper use of #define to aid in compile-time checking of strings (was KVC and KVO for arrays) Jonathan Dann Feb 16, 17:21