开发者

Core Data - retrieving attribute from a one-to-many relationship

Let's say that we have the following model:

Entity1: Person

relationship: jackets

Entity2: Jacket

attribute: size

relationship: owner

So 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:

  1. 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.

  2. 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜