NSManagedObject *parent = [NSEntityDescription insertNewObjectForEntityForName:@"Parent" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *child = [NSEntityDescription insertNewObjectForEntityForName:@"Child" inManagedObjectContext:self.managedObjectContext];
[child setValue:parent forKey:@"parent"];
That doesn’t seem to work for me, but I can do something like this:
[child setValue:"John" forKey:@"name"];
Thanks for the help!
If the relationship has been defined in the
ManagedObjectModelyou can useThis code example assumes that your Entity Parent has a relationship attribute set called childObject.
To use the . accessor for a property of a
NSManagedObjectyou will need to subclass it.Select your Entity and click Create
NSManagedObjectSubclass from the Editor MenuThen instead of Creating your objects as
NSManagedObjects, create them as Parent and Child classes