1

Mac OS X. CoreData app. NSTableView controlled by NSArrayController bound to managed object context for the Country entity. The Country entity has a 'name' attribute and a to-many relationship, 'branches', to a Branch entity. The Branch entity has a 'sales' attribute (an NSNumber).

The NSTableView has two NSTableColumns. The first shows the name of the Country. The second should show the total sales for that Country across all its Branches.

The first column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'name'. No problem there.

The second column's value is bound to the NSArrayController's arrangedObjects with a model key path of 'branches.@sum.sales'. This doesn't work. I get the error message: " addObserver:forKeyPath:options:context:] is not supported. Key path: @sum.sales"

If, instead, I add a 'totalSales' method to my Country class and the method is implemented as follows:

- (NSNumber *)totalSales
{
    return [[self branches] valueForKeyPath:@"@sum.sales"];
}

and I then bind the column to 'totalSales' it works fine. My understanding of the Collection Operators documentation is that this should be the same as simply binding to 'branches.@sum.sales'. I can't see why the latter doesn't work. Any ideas? I have seen similar questions in this and other forums, but have yet to see an explanation or solution.

jfewtr
  • 155
  • 1
  • 10

1 Answers1

0

I don't know if this is still topic for you, but it surely does need an answer.

The second column's value should be bound to NSArrayController in exactly the sam way as first. I don't know why you made it differently and what actually you wanted to achieve.

Your first task was to bind table columns to array columns and this works the same way for all the columns and types.

Second task is to get sum of certain NSTableColumn bound to certain other object, like NSTextfield. And this is done like this:

    [totalCountField bind: @"value" toObject: arrayController
          withKeyPath:@"arrangedObjects.@sum.price" options:nil];
mbpro
  • 2,460
  • 3
  • 22
  • 37
  • No, I'm not trying to put a column total into an NSTextField. That's easy and works fine. The problem I'm having is with binding column 2 of the NSTableView. I want that column to show the total sales for each country across all of that country's branches. But I do appreciate you trying to help. Thank you. – jfewtr Nov 14 '11 at 09:48
  • OK. I got it now. It seems to me you are trying to manage 2 entities with one controller of which the second is sub-entity. My guess would be that NSArrayController keypath cannot handle that, regarding to your solution, which clearly brings totalSales to Country level, t.i. to base entity – mbpro Nov 15 '11 at 12:05