0

I have an entity Item, and an entity Type (that has an attribute "Name") in a to-many relationship with Item. (Ie: Item: Brown Table, related to Type with Name "Coffee Table").

I've programmatically added new Items fine, using, for example:

[newItem setValue:([nameTextField stringValue]) forKey:@"Name"];
[newItem setValue:(costNumber) forKey:@"Cost"];
[newItem setValue:(priceNumber) forKey:@"Price"];

I've been searching for hours but can't find something that works for me adding a relationship to the new item. I'm using a NSPopUpButton to choose the Type of the item, and have tried methods like selectedItem, selectedTag, and selectedCell. I'm trying to get values from my "typeArray", which is filled as follows:

NSFetchRequest *fetchRequest2 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity2 = [NSEntityDescription entityForName:@"Type"
                                           inManagedObjectContext:managedObjectContext];
[fetchRequest2 setEntity:entity2];
NSError *error = nil;
typeArray = [managedObjectContext executeFetchRequest:fetchRequest2 error:&error];
if (typeArray == nil) {
    NSLog(@"ERROR");
}
[fetchRequest2 release];

I'm not sure if the following is along the right lines:

NSManagedObject *selectedType = [typeArray objectAtIndex:[typePopUpButton selectedTag]];

But then I have no option for selectedType to add something like "addObject"..

Any help appreciated, thank you.

D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67
  • I'm trying to use http://lethain.com/one-to-many-relationships-in-coredata/ but can't apply it to my situation with the pop up button. – D'Arcy Rail-Ip Feb 09 '12 at 15:27
  • You didn't mention in your question if you'd tried [newItem mutableSetValueForKey:@"Type"] - the NSMutableSet you'll get from that should receive the addObject: message. – Maurice Kelly Feb 09 '12 at 20:58
  • I tried `NSMutableSet *mutableSet = [newItem mutableSetValueForKey:@"Type"]; [mutableSet addObject:newItem];` , which resulted in this error: `-[__NSCFSet entity]: unrecognized selector sent to instance 0x102819c90 ` – D'Arcy Rail-Ip Feb 09 '12 at 21:32
  • Actually, I know why it doesn't work, it's not connected to my NSPopUpButton selection. I believe that is where my problem is, I can't connect between the choice selected from the pop up button to selecting the same Name attribute from the Type entity. – D'Arcy Rail-Ip Feb 09 '12 at 21:35
  • Break your code down into multiple lines and use the debugger to see what you've got at each stage. And instead of retrieving all objects into your `typeArray` you could maybe use a property of your pop up button to construct your fetch predicate. – Maurice Kelly Feb 10 '12 at 07:50

1 Answers1

0

This is what I ended up using:

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type"
                                          inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSError *error = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name like %@", [typePopUpButton titleOfSelectedItem]];
[fetchRequest setPredicate:predicate];
NSArray *typeSet = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (typeSet == nil) {
    NSLog(@"ERROR");
}
[fetchRequest release];


NSManagedObject *typeObject = [typeSet objectAtIndex:0];
[typeObject addItemsObject:newItem];

Basically, the object needs to be fetched so that a relationship can be made between the two items, and the predicate is based on the typePopUpButton's titleOfSelectedItem method.

I ensure to only select one object with the method [objectAtIndex:0].

It does bring up a warning though: NSManagedObject may not respond to 'addItemsObject'.

However this does work for me.

D'Arcy Rail-Ip
  • 11,505
  • 11
  • 42
  • 67