Le 30 janv. 09 à 20:14, Jean-Nicolas Jolivet a écrit :

I've been working on my first CoreData project and I have a question regarding dynamically generated accessors for to-many relationships and KVC...

I'll keep it as simple as possible, let's assume I have a Department entity which contains many Employees entities.... from what I understand in the doc, CoreData should automatically generate those methods for Department:

-addEmployees:(NSSet *)values
-addEmployeesObject:(NSManagedObject *)value

My question is, how would I access these 2 methods using KVC accessors?

For example, if I want to set the employee's name attribute I can use: [anEmployee setValue:@"John Doe" forKey:@"name"];

Following this pattern, could I use
[aDeparment setValue:anEmployee forKey@"employees]; // Assuming anEmployee is an NSManagedObject*

or

[aDeparment setValue:someEmployees forKey@"employees]; // Assuming someEmployees is an NSSet*


In other words, are relationship also KVC and, will CoreData aumatically call the correct method depending on the type of the "value" I'm setting (either an NSSet or an NSManagedObject) ?

No. I never tried, but I'm pretty sure you will obtain garbage, at least for you first proposition. The second will simply replace the old employees set with someEmployees.

I know I can declare an NSManagedObject category with a bunch of accessors etc.. to suppress compiler warning but I was wondering about the KVC get/set as I am using them too....

If you want to use the KVC method use -mutableSetValueForKey or - mutableSetValueForKeyPath: . In return you have a proxy object for the key (keypath). Send to this proxy usual mutable set methods, the KVO notifications will be send for you. EX :

[[aDepartment mutableSetForKey:@"employees"] addObject:someEmployees];
[[aDepartment mutableSetForKey:@"employees"] unionSet:someEmployees];

Frédéric


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to