将项目添加到NSSet对于核心数据一对多关系 [英] Adding An Item To An NSSet For A Core Data One To Many Relationship

查看:111
本文介绍了将项目添加到NSSet对于核心数据一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据关系,其中一个实体持有许多另一个实体。据我所知,许多类的每个实例都在一个NSSet内部?里面的一个类。 (?)

I have a core data relationship where one entity holds many of another entity. As far as I am aware each instance of the many class is held inside an NSSet? inside the one class. (?)

我的问题是 - 将项目添加到此集合的最佳方式是什么?我认为这必须是一个很常见的问题 - 但我似乎找不到一个简单的方法。

My question is - what is the best way to add items to this set? I figure this must be a very common problem - but I cannot seem to find an easy method.

这是我的尝试:(这是从一个类)

This is my attempt: (This is all taken from the one class)

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是我想添加的许多对象。

Where retrieveValues() just updates all the coreData values in the class. TimeSlot is the many object which I want to add.

我在最后一行得到一个错误,错误是:无法调用类型Set< ; _>带有数组类型列表的参数

I get an error on the last line, the error is: "cannot invoke initializer for type Set<_> with an argument of list of type Array"

我在概念上错了吗?感谢!

Am I conceptually wrong at all? Thanks!

推荐答案

您已声明 timeSlotItems saveTimeSlot:为静态,所以我不知道你的意图是什么。我怀疑这不是你需要的。

You've declared both timeSlotItems and saveTimeSlot: as static, so I'm not sure what your intention is there. I suspect it's not what you need.

以同样的方式Core Data自动运行时生成属性的优化访问器,它也生成关系访问器。

In the same way that Core Data automatically runtime-generates optimized accessors for attributes, it also generates accessors for relations.

你不说多对多关系的one一侧的名字,但如果我假设它是 Schedule code>,其中计划 TimeSlot 具有一对多关系,称为 timeSlotItems ,那么Core Data将在运行时为您生成以下访问器:

You don't say what the name of the "one" side of the to-many relation is, but if I assume that it's something like Schedule, where Schedule has a to-many relation to TimeSlot called timeSlotItems, then Core Data will runtime-generate the following accessors for you:

class Schedule: NSManagedObject {
    @NSManaged public var timeSlotItems: Set<TimeSlot>
    @NSManaged public func addTimeSlotItemsObject(value: TimeSlot)
    @NSManaged public func removeTimeSlotItemsObject(value: TimeSlot)
    @NSManaged public func addTimeSlotItems(values: Set<TimeSlot>)
    @NSManaged public func removeTimeSlotItems(values: Set<TimeSlot>)
}

这篇关于将项目添加到NSSet对于核心数据一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆