[英]How do I convert my Card to an NSSet?
I am trying to add a new Card
to a Deck
of cards, but am running into an issue when trying to save my card to the deck's cards
. 我想一个新的添加
Card
到Deck
的卡,而是试图我的名片保存到甲板的时候我遇到了一个问题cards
。 How do I convert it to an NSSet
? 如何将其转换为
NSSet
?
func saveQA(question: String, answer: String) {
let currentDeckName = deckName
let entity = NSEntityDescription.entityForName("Card", inManagedObjectContext: managedContext)
let newQA = Card(entity: entity!, insertIntoManagedObjectContext: managedContext)
newQA.question = question
newQA.answer = answer
currentDeckName!.cards = newQA
do {
try managedContext.save()
userCards.append(newQA)
}
catch {
let saveError = error as NSError
print(saveError)
}
}
Here, you're trying to assign a Card
to a property that is expecting an NSSet
. 在这里,您尝试将
Card
分配给需要NSSet
。 A card is not a set (although it could be an object in a set). 卡不是集合(尽管它可以是集合中的对象)。
currentDeckName!.cards = newQA
Taking advantage of type safety 利用类型安全
Since you are coding in Swift, the first thing you should do is use Swift types such as Set
instead of NSSet
. 由于您使用Swift进行编码,因此您应该做的第一件事是使用Swift类型(例如
Set
而不是NSSet
。 In your Deck+CoreDataProperties.swift
, change your relationship from NSSet?
在您的
Deck+CoreDataProperties.swift
,从NSSet?
更改关系NSSet?
to a typed set of cards. 到一组类型的卡。
@NSManaged var cards: Set<Card>
This not only tells Swift that cards
is a Set, but also specifies that the objects in the set will be Card
s. 这不仅告诉Swift
cards
是Set,还指定集合中的对象将是Card
。
You always want to declare specific types for relationships to benefit from the language's type safety. 您总是想声明关系的特定类型,以便从语言的类型安全中受益。 This allows the compiler to prevent you from ever adding anything other than a
Card
to the deck's set of cards. 这样,编译器就可以防止您将
Card
以外的任何东西添加到卡座的卡集中。
Adding a card to the deck 将卡片添加到卡座
You could use the usual Set
methods such as insert
to insert a card into a deck's set. 您可以使用通常的
Set
方法,例如insert
,将卡片插入卡组中。
currentDeckName.cards.insert(newQA)
However, a much easier way is to use the reverse relationship on the card itself. 但是,一种更简单的方法是在卡本身上使用反向关系。 In other words, tell the new card that it belongs to this deck:
换句话说,告诉新卡它属于此卡组:
newQA.deck = currentDeckName
That will automatically add the card to the deck's set of cards. 这会将卡自动添加到卡座的卡组中。
It's a bit less code, and less is generally better to read and understand. 它的代码更少了,阅读和理解起来通常更好。
Speaking of readability, you may want to consider renaming currentDeckName
to currentDeck
since that object is a Deck
, not the name of a deck. 说到可读性,您可能要考虑将
currentDeckName
重命名为currentDeck
因为该对象是Deck
,而不是Deck
的名称。
For converting your object to MutableSet
please verify with following step. 要将对象转换为
MutableSet
请通过以下步骤进行验证。
1) Select your model object
1)选择
model object
2) select your parent entity
2)选择您的上级
entity
3) Select Relationship
which is one To many
3)选择
one To many
Relationship
4) now check following image property
is one To many
or not. 4)现在检查以下图像
property
是否one To many
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.