5

Ok so i'm still quite new to iOS and confused about a few things. Firstly, here are my entities... E.R.D

What i have already in there data wise is a Fruit (Apple) and a Source (Tree). They are both saved in the database.

Next i want to add an orange, but have the relationship with 'Tree'. So this is what i am using:

    Fruit *fruit = (Fruit *)[NSEntityDescription insertNewObjectForEntityForName:@"Fruit" inManagedObjectContext:managedObjectContext];
    fruit.fruitName = @"Orange";
  NSSet *test = [NSSet setWithObject:fruit];
    [_source addSourceFruit:test];

NSLog(@"4");

fruit.fruitSource = _source;

(_source is the 'Tree', I performed a fetch request for 'Tree' on the Source entity into an array, then took objectAtIndex:0 (Tree) and assigned it to a point towards source entity.

   data = [managedObjectContext executeFetchRequest:request error:&error];


   Source *_source = [data objectAtIndex:0];

and the accessor methods:

- (void)addSourceFruitObject:(Fruit *)value;
- (void)removeSourceFruitObject:(Fruit *)value;
- (void)addSourceFruit:(NSSet *)values;
- (void)removeSourceFruit:(NSSet *)values;

I have found an answer relating to bundles but i'm not entirely sure about them. I have read this 'https://developer.apple.com/library/mac/#documentation/CoreFOundation/Conceptual/CFBundles/AboutBundles/AboutBundles.html' and 'https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html' but i just don't seem to be grasping it brilliantly.

EDIT: The error is

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSSet intersectsSet:]: set argument is not an NSSet'
*** First throw call stack:
4

1 回答 1

3

I believe you're confusing Core Data by replacing the sourceFruit set. Core Data maintains inverse relationships. That means you only have to set fruit.fruitSource = _source; for it to understand how the objects are to be connected. You could use [_source addSourceFruitObject:fruit]; instead but that seems less concise to me.

(Aside: it's a good idea to avoid using the _name style of naming for local variables. It has become something of a standard to use that form for instance variables that back properties.)

于 2012-12-10T13:01:58.063 回答