【发布时间】:2015-12-16 12:19:00
【问题描述】:
我有一个核心数据关系,其中一个实体拥有许多另一个实体。据我所知,许多类的每个实例都保存在 NSSet 中?在一个班级里面。 (?)
我的问题是 - 将项目添加到此集合的最佳方式是什么?我认为这一定是一个非常普遍的问题 - 但我似乎找不到简单的方法。
这是我的尝试:(这都取自一个类)
static var timeSlotItems: NSSet? //The Set that holds the many?
...
static func saveTimeSlot(timeSlot: TimeSlot) { //TimeSlot is the many object
retrieveValues()
var timeSlotArray = Array(self.timeSlotItems!)
timeSlotArray.append(timeSlot)
var setTimeSlotItems = Set(timeSlotArray)
self.timeSlotItems = setTimeSlotItems // This is the error line
}
retrieveValues() 只更新类中的所有 coreData 值。 TimeSlot 是我要添加的许多对象。
我在最后一行得到一个错误,错误是:“无法使用数组类型列表的参数调用 Set<_> 类型的初始化程序”
我在概念上完全错了吗?谢谢!
【问题讨论】:
-
一对多还是多对多?
-
静态函数和self?