• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: insertObject:in<Key>AtIndex: not being called.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: insertObject:in<Key>AtIndex: not being called.


  • Subject: Re: insertObject:in<Key>AtIndex: not being called.
  • From: Paul Szego <email@hidden>
  • Date: Sun, 3 Apr 2005 21:31:45 +1200


Ok - so the bottom line seems to be: if you're wanting KVC write access, you have to know whether you're dealing with a to-many property. In that case, use mutableArrayValueForKeyPath: instead of just setValue:forKey: to cater for the situation where indexed accessors are provided.


Which leads to me next question about bindings, which I've posted elsewhere. If I bind a to-many property that's providing indexed accessors, the default bind: behaviour provided in NSObject tries to call setValue:forKey: instead of using mutableArrayValueForKeyPath:, and so I get a "not KVC compliant" error message. Is this what should happen?

Thanks, Paul.

On 03/04/2005, at 1:53 PM, mmalcolm crawford wrote:


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

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden


This email sent to email@hidden



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: insertObject:in<Key>AtIndex: not being called.
      • From: mmalcolm crawford <email@hidden>
References: 
 >insertObject:in<Key>AtIndex: not being called. (From: Paul Szego <email@hidden>)
 >Re: insertObject:in<Key>AtIndex: not being called. (From: mmalcolm crawford <email@hidden>)

  • Prev by Date: Re: Calling methods on other applications?
  • Next by Date: Re: insertObject:in<Key>AtIndex: not being called.
  • Previous by thread: Re: insertObject:in<Key>AtIndex: not being called.
  • Next by thread: Re: insertObject:in<Key>AtIndex: not being called.
  • Index(es):
    • Date
    • Thread