Re: valueForKey: on collections [was: Re: Using isMemberOfClass with a tree of subclass of NSManagedObject]
Re: valueForKey: on collections [was: Re: Using isMemberOfClass with a tree of subclass of NSManagedObject]
- Subject: Re: valueForKey: on collections [was: Re: Using isMemberOfClass with a tree of subclass of NSManagedObject]
- From: "Kyle Sluder" <email@hidden>
- Date: Sat, 28 Jun 2008 23:19:33 -0400
On Sat, Jun 28, 2008 at 9:35 PM, Owen Yamauchi
<email@hidden> wrote:
> On Sat, Jun 28, 2008 at 6:24 PM, Ben Trumbull <email@hidden> wrote:
>>> Which is, frankly, a bit goofy and, thus, the primary reason why I
>>> would avoid using -valueForKey: on a dictionary.
>>
>> Amen. It's slower, and people reading your code can get very confused.
>
> Then is there a recommended way of binding to the contents of
> dictionaries? For example, suppose I have an NSObjectController
> representing some object, which has an NSDictionary (let's say it's
> called "attributes") as a property. Then can I bind to the value
> stored under a key in that dictionary? Say I want to bind a text
> field's value to whatever is stored under the key "name" in the
> "attributes" dictionary. The intuitive (to me) thing to do is bind to
> the NSObjectController, controller key "selection", model key path
> "attributes.name". But that won't work. Is there a good solution?
Why wouldn't it? The NSObjectController's selection proxy receives
the keypath "attributes.name", so it sends
-valueForKeyPath:@"attributes.name" to its concrete object (your model
object). This object's implementation of -valueForKeyPath: sees that
"attributes" is a valid key, so it gets your NSDictionary exposed as
"attributes" and sends it -valueForKeyPath:@"name".
-valueForKeyPath:'s custom logic treats "name" as a dictionary key
first, and returns -objectForKey:@"name".
At least this is what I've gathered from Bill and Ben's discussion.
--Kyle Sluder
_______________________________________________
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