views:

61

answers:

2

When I define a to many relationship between entities in Xcode and then generate the data class from the entity I get a header with the following methods defined:

@interface PriceList (CoreDataGeneratedAccessors)
- (void)addItemsObject:(PriceListItem *)value;
- (void)removeItemsObject:(PriceListItem *)value;
- (void)addItems:(NSSet *)value;
- (void)removeItems:(NSSet *)value;
@end

When I attempt to call addItemsObject with the following code a doesNotRecognizeSelector exception is thrown.

PriceListItem *item = [NSEntityDescription insertNewObjectForEntityForName:@"PriceListItem" inManagedObjectContext:managedObjectContext];
item.cat = [attributeDict valueForKey:@"c"];
item.sel = [attributeDict valueForKey:@"s"];
[self addItemsObject:item];

From what I have read I do not have to implement these methods and that they are generated at runtime. Any ideas?

self in this context is a subclass of PriceList.

I think that the problem is that I am not initializing the PriceList object appropriately.

A: 

You need to tell your model about the new subclass you just generated. Look in your model, select the entity and on the right change the class from NSManagedObject to the name of your subclass. Otherwise Core Data just hands you back a NSManagedObject and does not know your subclass exists.

Update

Did you verify? Since Xcode will edit the data model but if you do not save it afterwards then it would not be persisted. What happens when you look at the data model?

Marcus S. Zarra
From what I can tell this was done for me when I generated the class.
BLeB
But I bet the exception mentions NSManagedObject, which is exactly what would happen in the situation Marcus describes.
Tom Harrington
A: 

The simplest explanation is that the self class isn't actually a subclass of PriceList. I would log

[[self super] class]

...to confirm.

You should also make sure that the interface for the methods is in the .h header file and not the .m implementation file. If you're generating the methods from the contextual menu in the model editor its easy to park them in the wrong place.

TechZen