Core Data - retrieving attribute from a one-to-many relationship
Let's say that we have the following model:
Entity1: Person
relationship: jacketsEntity2: Jacket
attribute: size relationship: ownerSo we have:
owner <-->> jackets (each person can have many jackets, but a jacket can only have one owner)Let's set up a NSTableView with 2开发者_JS百科 columns. I bind the first one to show all persons. Then I want the second column to show the jackets' sizes owned by the selected owner in the first column. To get the first column is trivial, but how do I bind the second? Do I need another NSArrayController to represent Jackets entities or can I do it directly using something similar to (this didn't work) binding the column to Persons NSArrayController and under value bind controller key: selection & Model Key Path: arrangedObjects.size. When I try this i get the error:
[ addObserver: forKeyPath:@"arrangedObjects.Jacket.size" options:0x0 context:0x0] was sent to an object that is not KVC-compliant for the "arrangedObjects" property.
Thanks, Hans
You can loop through the Jackets of a person just by calling aPerson.jackets:
NSMutableString *allSizes = [NSMutableString string];
for (Jacket *jacket in aPerson.jackets){
allSizes = [allSizes stringByAppendingString: jacket.size];
}
//Show allSizes in the second column
I finally solved it. I did two mistakes:
I was trying to plot a tree like structure in one NSTableView. Since this did not support that I had 3 instances in the first and hundreds in the second table.
I had to bind a new NSArrayController to the second NSTableView and bind its content set to the selection of my Jackets NSArrayController.
That solved it!
精彩评论