Saving CoreData to-many relationships in Swift

余生长醉 提交于 2019-11-27 18:36:16

In a one-to-many relationship, it is easier to set the "to-one" direction of the inverse relationships, in your case just

list.board = board

so that the extension methods are actually not needed here.

You should invoke addListObject(...) on board object:

board.addListObject(list) // notice that we pass just one object

Additionaly, if you want to be able to add a set of lists to particular board object, you can enhance you Board class extension with methods that accept set of objects:

func addList(values: NSSet) {
    var items = self.mutableSetValueForKey("lists");
    for value in values {
        items.addObject(value)
    }
}

func removeList(values: NSSet) {
    var items = self.mutableSetValueForKey("lists");
    for value in values {
        items.removeObject(value)
    }
}

If you define:

@NSManaged var lists: Set<List>

Then you can do:

board.lists.insert(list)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!