目标是拥有一个列表,该列表与该列表中的项目集合有关系。另一个堆栈溢出帖子告诉我,我需要这种关系来存储项目列表,但我不知道如何将项目添加到特定列表。我也不知道如何保存它们。
任何建议、代码示例或教程都会非常有帮助。谢谢。
最佳答案
首先创建“List”对象,然后需要创建“ItemList”对象,将它们添加到“List”对象并保存:
//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];
关于ios - 在核心数据 swift 中使用 one -> may 关系保存数据集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180821/