Re: Show count of Core Data to-many relationship in NSTableView cell with bindings?
Re: Show count of Core Data to-many relationship in NSTableView cell with bindings?
- Subject: Re: Show count of Core Data to-many relationship in NSTableView cell with bindings?
- From: Ken Thomases <email@hidden>
- Date: Fri, 06 Feb 2015 02:03:34 -0600
On Feb 6, 2015, at 1:43 AM, Steve Mills <email@hidden> wrote:
> I have a Core Data entity called Keyword that has a to-many relationship to another entity called Image. In a table that displays these Keywords, I want one column to show the number of Images that use that particular Keyword. I tried setting the Value binding for the Table View Cell to bind to the Table Cell View with model key path of objectValue.images.count. That doesn't work - throws an exception:
>
> [<_NSFaultingMutableSet 0x6200000246e0> addObserver:forKeyPath:options:context:] is not supported. Key path: count
>
> Is it not possible to bind to an entity's relationship properties? The Keyword class has a property named assets of class NSSet, so it seems like the binding should be able to access it just like the attribute properties.
You can't use KVO or bindings through arrays or sets, even to their immediate properties.
For KVC, if you were to attempt to do [theTableCellView valueForKeyPath:@"objectValue.images.count"], that would attempt to get the "count" property of each _element_ of objectValue.images and return a set containing all of those counts (wrapped in NSNumbers). In theory, if KVO through sets worked, then observing the key path objectValue.images.count would not notify you when the count of objectValue.images changed. It would notify you when the set returned by [theTableCellView valueForKeyPath:@"objectValue.images.count"] changed (or might have changed). That's not what you want, anyway.
However, there are collection operators <https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html>, including @count. You can bind to that with model key path objectValue.images.@count.
Regards,
Ken
_______________________________________________
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