memmons
memmons

Reputation: 40492

iPhones SDK: Setting a relationship property object using core data?

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?

Upvotes: 0

Views: 244

Answers (1)

gerry3
gerry3

Reputation: 21460

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";

Upvotes: 1

Related Questions