Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.fromBundle = [NSSet setWithObject:aBundle];

(yes, all changes were saved to the database)

am I missing something? Thanks

like image 478
Duck Avatar asked Nov 14 '22 04:11

Duck


1 Answers

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];
like image 99
ughoavgfhw Avatar answered Dec 28 '22 07:12

ughoavgfhw