0
votes

I am having errors trying to run this app with core data. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "ingredient"; desired type = NSSet; given type = Ingredient;

I see that the core-generated accessors addIngredient: and removeIngredient: both take an NSSet as a parameter, so I tried changing that to Ingredient, but that didn't help. I suspect that although I change the parameter in the header, there is something going on in implementation that I can't see? Suggestions?

Recipe.h

@class IngredientList;

@interface Recipe : NSManagedObject

@property (nonatomic, retain) NSString * recipeName;
@property (nonatomic, retain) IngredientList *ingredientList;

@end

IngredientList.h

@class Ingredient, Recipe;

@interface IngredientList : NSManagedObject

@property (nonatomic, retain) Recipe *recipe;
@property (nonatomic, retain) NSSet *ingredient;
@end

@interface IngredientList (CoreDataGeneratedAccessors)

- (void)addIngredientObject:(Ingredient *)value;
- (void)removeIngredientObject:(Ingredient *)value;
- (void)addIngredient:(NSSet *)values;
- (void)removeIngredient:(NSSet *)values;

@end

Ingredient.h

@class IngredientList;

@interface Ingredient : NSManagedObject

@property (nonatomic, retain) NSString * ingredientName;
@property (nonatomic, retain) IngredientList *ingredientList;

@end
1
Did the answer help? Let me know if you need more information! - Martin R

1 Answers

0
votes

If I see it correctly then ingredient is a to-many relationship from IngredientList to Ingredient. (Remark: A better name would be the plural form ingredients.)

Then addIngredientObject:, not addIngredient: is the method to add an ingredient to a list:

IngredientList *aList = ...;
Ingredient *anIngredient = ...;
[aList addIngredientObject:anIngredient];

Alternatively, you can set the inverse relationship:

anIngredient.ingredientList = aList;

The method addIngredient:, which takes an NSSet as argument, is used to add an entire set of objects to the relationship in one step.

The Core Data accessor methods are generated dynamically at runtime, therefore you cannot just change the generated .h or .m file. You should always let Xcode re-generate the files if the model has been changed.