I have a managed object subclass named Tag that has a to-many relationship to another managed object subclass Card; in my code I retrieve a Tag from the store, and then access the associated Card objects and add them to an array. I also have another managed object subclass CardVariation that is added to the array.
Both Card and CardVariation conform to a TaggedCard protocol; when I access the objects stored in the array, I cast them to the TaggedCard protocol:
id<TaggedCard> x = (id<TaggedCard>)[theArray objectAtIndex:i];
I then attempt to send a TaggedCard message to the object:
NSString *y = [x taggedCardName];
When this runs, I get an exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSManagedObject taggedCardName]: unrecognized selector sent to instance 0x4e7c620'
I can cast my object to a Card (or CardVariation) with no problem; is there something special with casting a managed object subclass to a protocol?
It sounds like x isn’t an instance of the class you’re expecting. The cast avoids a compiler warning, but if the instance x were to respond to taggedCardName, your code would work regardless.
What’s the output of this?
If you don’t get the class you’re expecting (e.g.
<Card: 0x4e72090> (entity: Card; ...), you have an instance of the wrong class. Here are two things to check:Make sure your subclass .m files and your data model are listed in Build Phases > Compile Sources for your target.
In the data model, under the entity, make sure your subclass is listed as the Class, and not NSManagedObject.