开发者

iPhone - core data NSSet relationship

I have two entities Books and Bundles. A book can belong to none, to one or more bundles. So I have a bi-directional to-many relationship between these entities.

I am about to setup the value of this relationship on the Book part.

If the attribute was a regular one, I would simply do something like

aBook.title = @"this is my title"; 

If I wanted to set the title for a book. But as this is a bi-directional to-many relationship, core data says it is a NSSet attribute.

My question is: how do I set and read values from this attribute?

I first thought it was a regular NSSet. So, after reading the product I simple did

NSSet *aSet = Book.fromBundle;

but for my dismay aSet contained zero objects

So I thought I was setting it in error. I set it earlier doing

Book.from开发者_如何转开发Bundle = [NSSet setWithObject:aBundle];

(yes, all changes were saved to the database)

am I missing something? Thanks


It is a normal set, but core data advises against directly assigning a new set (for this very reason). If you are using a custom class, the standard implementation would include addFromBundleObject: and removeFromBundleObject: methods for you to use. If you do not use a custom class, or choose not to implement these methods, then you can use the mutableSetValueForKey: method to get a set which you can modify.

NSMutableSet *mutableSet = [book mutableSetValueForKey:@"fromBundle"];
[mutableSet addObject:aBundle];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜