My Core Data object model has three nested objects as shown below:
Item
Beverage
Brand
When I first create an instance of Item
Item *item = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:self.objectContext];
the item.beverage property is nil. Next I want to store a value in the item.beverage.brand.title property.
Do I have to create an instance of Beverage and assign it to item.beverage, then create an instance of Brand and assign it to item.beverage.brand.
item.beverage = [NSEntityDescription insertNewObjectForEntityForName:@"Beverage" inManagedObjectContext:self.objectContext];
item.beverage.brand = [NSEntityDescription insertNewObjectForEntityForName:@"Brand" inManagedObjectContext:self.objectContext];
before I can finally assign the value to the title property?
item.beverage.brand.title=@"Sample Title";
Is there a shorter/less verbose way to do this?
You need to create each object in the object graph. I’m not aware of any core-data provided shortcuts. You could of course write your own methods, categories, or macros to reduce the verbosity if you find your self writing a lot of boiler plate code.