Skip navigation.
 
mlRe: insertObject:in<Key>AtIndex: not being called.
FROM : mmalcolm crawford
DATE : Sun Apr 03 03:53:21 2005

On Apr 2, 2005, at 2:47 PM, Paul Szego wrote:

> I can't get KVC working for to-many relationships when I don't 
> expose the NSMutableArray directly. If I provide the following 
> indexed accessors (for a property named "items"), it doesn't work:
> -countOfItems and -objectInItemsAtIndex:
> -insertObject:inItemsAtIndex: and -removeObjectFromItemsAtIndex:
> If I make the "items" attribute public, or provide get/set methods, 
> things work. According to the documents however, this class *is* 
> KVC compliant. In that case, I should be able to use valueForKey: 
> and setValue:forKey: for this property.
>

The documentation is a little misleading or inconsistent (I've filed 
a bug report -- and another for the misleading error report), however 
the description for setValue:forKey: (<http://developer.apple.com/
documentation/Cocoa/Reference/Foundation/ObjC_classic/Protocols/
NSKeyValueCoding.html
>) is explicit about what methods you must 
implement for it to work -- i.e. you must implement a set<Key>: 
method or allow access to an appropriately-named instance variable.

If you have only implemented -countOfItems, -objectInItemsAtIndex:, -
insertObject:inItemsAtIndex:, and -removeObjectFromItemsAtIndex:, 
then your class *is* KVC compliant, however the NSKeyValueCoding 
accessor for the collection is mutableArrayValueForKey: ...

> "Implementing indexed accessors methods for a property makes that 
> property indistinguishable from an array when using key-value 
> coding methods."


The documentation does go on to say,

"Implementing [countOfTransactions and objectInTransactionsAtIndex:] 
allows your application to invoke valueForKey: using the key for the 
to-many property. An array containing the objects in the relationship 
is returned.
[...]
The key-value coding methods mutableArrayValueForKey: and 
mutableArrayValueForKeyPath: provide mutable access to a to-many 
relationship..."


mmalc

Related mailsAuthorDate
mlinsertObject:in<Key>AtIndex: not being called. Paul Szego Apr 3, 00:47
mlRe: insertObject:in<Key>AtIndex: not being called. mmalcolm crawford Apr 3, 03:53
mlRe: insertObject:in<Key>AtIndex: not being called. Paul Szego Apr 3, 11:31
mlRe: insertObject:in<Key>AtIndex: not being called. mmalcolm crawford Apr 3, 11:51
mlRe: insertObject:in<Key>AtIndex: not being called. mmalcolm crawford Apr 3, 11:55
mlRe: insertObject:in<Key>AtIndex: not being called. Paul Szego Apr 4, 22:13