CoreData add object to a to-many Relationship error

227 views Asked by At

I have a CoreData object Player with a to-many relationship to goals. I am trying to add a goal object like so,

Goal *fg =(Goal*)[self.database createGoalObject]; //Custom function 

Player* player = (Player*)[NSEntityDescription insertNewObjectForEntityForName:@"Player" inManagedObjectContext:self.database.managedObjectContext];

[player addGoalsObject:fg];

My app is breaking with the following error:

'NSInvalidArgumentException', reason: '-[__NSCFSet entity]: unrecognized selector sent to instance 0x8d90f00'

A po at the debug prompt shows that 0x8d90f00 is a Goal object. My questions are:

  1. why is core data sending a message to the Goal object?
  2. why is the goals relationship for Player initializing to nil? Should it not be an empty set till loaded?
  3. Do I have to override the addGoalsObject in Player.h to manually set the value?
1

There are 1 answers

1
Trevor Squires On

The error specifically says that something tried to call the entity method on an __NSCFSet. In other words, the object at 0x8d90f00 is an NSSet of some type.

You are being slightly mislead by the output of po 0x8d90f00 because NSSet's description method will include the description of the objects it contains, wrapped in {( and )} characters.

Had you done po [0x8d90f00 class] it would have responded NSSet or NSMutableSet.

Your createGoalObject is returning a set.