ios 在 Swift 中保存 CoreData 对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25127090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving CoreData to-many relationships in Swift
提问by Pirijan
I have a one-to-many relationship that looks like so,
我有一个一对多的关系,看起来像这样,
I've set up my model classes in a file to match:
我已经在文件中设置了我的模型类以匹配:
import CoreData
import Foundation
class Board: NSManagedObject {
@NSManaged var boardColor: String
@NSManaged var boardCustomBackground: AnyObject?
@NSManaged var boardID: String
@NSManaged var boardName: String
@NSManaged var lists: NSSet
}
class List: NSManagedObject {
@NSManaged var listID: String
@NSManaged var listName: String
@NSManaged var board: Board
}
Because I'm fetching data from multiple JSON endpoints, I have to save my lists seperately from my boards. What I want to do is create/update a list for a board with a matching boardID.
因为我从多个 JSON 端点获取数据,所以我必须将我的列表与我的板分开保存。我想要做的是为具有匹配 boardID 的板创建/更新列表。
Here's where I am after multiple attempts:
这是我多次尝试后的位置:
func saveList(boardID: String, listName: String, listID: String) {
let request = NSFetchRequest(entityName: "Board")
var error: NSError? = nil
request.predicate = NSPredicate(format: "boardID like %@", boardID)
let results: NSArray = context.executeFetchRequest(request, error: &error)
if results.count > 0 {
for result in results {
let board = result as Board
let list = NSEntityDescription.insertNewObjectForEntityForName("List", inManagedObjectContext: context) as List
println(" want to save \(listName) in \(board.boardName)")
board.lists.addListObject(lists)
list.listName = listName
list.listID = listID
}
}
}
Based on Defining CoreData Relationships in Swiftand this, I tried to implement @Keenle's answer for define list objects inside a board:
基于在 Swift和this 中定义 CoreData 关系,我尝试实现@Keenle 的答案,用于在板内定义列表对象:
import Foundation
extension Board {
func addListObject(value:List) {
var items = self.mutableSetValueForKey("lists");
items.addObject(value)
}
func removeListObject(value:List) {
var items = self.mutableSetValueForKey("lists");
items.removeObject(value)
}
}
However, I ran into the following error at board.lists.addListObject(lists)
:
'NSSet' does not have a member named 'addListObject'`
但是,我在以下位置遇到了以下错误board.lists.addListObject(lists)
:
“NSSet”没有名为“addListObject”的成员
Instead of board.lists.addListObject(lists)
, I also tried board.lists.listName = listName
as implied in this Obj-C example, but that sadly didn't work either.
取而代之的是board.lists.addListObject(lists)
,我也board.lists.listName = listName
按照这个Obj-C 示例中的暗示进行了尝试,但遗憾的是,这也不起作用。
(Also, The println
output is correctly specifying the right board and list.)
(此外,println
输出正确指定了正确的板和列表。)
Thanks in advance!
提前致谢!
回答by Martin R
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.
所以这里实际上不需要扩展方法。
回答by Keenle
You should invoke addListObject(...)
on board
object:
您应该addListObject(...)
在board
对象上调用:
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:
此外,如果您希望能够向特定board
对象添加一组列表,您可以Board
使用接受一组对象的方法来增强类扩展:
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)
}
}
回答by FranMowinckel
If you define:
如果你定义:
@NSManaged var lists: Set<List>
Then you can do:
然后你可以这样做:
board.lists.insert(list)