views:

42

answers:

1

I'm using core data in my app. I have two entities that are related: EntityA and EntityB. EntityA has a property of type "relationship" with EntityB. In addition, both of these entities are defined classes (not the default NSManagedObject). I'm inserting a new object into my data like this:

EntityA *newEntityA = [NSEntityDescription insertNewObjectForEntityForName:@"EntityA" inManagedObjectContext:self.managedObjectContext];

newEntityA.name = @"some name";
newEntityA.entityB.name = @"some other name";

The problem is entityB.name is null. Even if I add an NSLog() statement right after assigning the value, it is null. What is the proper way of setting my "name" property of EntityB when EntityB is a property of EntityA?

+1  A: 

You need to also create an EntityB object first:

EntityA *newEntityA = [NSEntityDescription insertNewObjectForEntityForName:@"EntityA" inManagedObjectContext:self.managedObjectContext];

newEntityA.name = @"some name";

EntityB *newEntityB = [NSEntityDescription insertNewObjectForEntityForName:@"EntityB" inManagedObjectContext:self.managedObjectContext];

newEntityA.entityB = newEntityB;
newEntityA.entityB.name = @"some other name";
gerry3
Thank you. Sometimes it is the obvious things which escape us.
Harkonian