目的是建立一个列表,该列表与该列表中的项目集合有关系。另一个堆栈溢出帖告诉我,我需要这种关系来存储项目列表,但我不知道如何将项目添加到特定列表。我也不知道如何保存它们。
任何建议,代码示例或教程都会非常有用。感谢。
答案 0 :(得分:1)
首先创建了你的" List"对象,那么你需要创建" ItemList"对象,将它们添加到您的"列表"对象并保存:
//Create List (if you didn't created it already).
List *yourListObject = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:yourManagedContext];
//Create as many ListItems as you need
ItemList *item1 = [NSEntityDescription insertNewObjectForEntityForName:@"ItemList" inManagedObjectContext:yourManagedContext];
ItemList *item2 = [NSEntityDescription insertNewObjectForEntityForName:@"ItemList" inManagedObjectContext:yourManagedContext];
//and so on ...
//You can add then one by one
[yourListObject addListItemObject:item1];
[yourListObject addListItemObject:item2];
//OR all at once
NSSet *itemsSet = [NSSet setWithObjects:item1,item2, nil];
[yourListObject addListItems: itemsSet];
//Save
NSError *saveError = nil;
[yourManagedContext save:&saveError];