1

I'am having trouble with adding an Array [Categories] into my Questions entity relationship

enter image description here

The categories relationship being a "to many" it is an NSSet, so I would like to add my [Categories] array into this NSSet.

I have tried something like this, but nothing gets in.

newQuestion.categories?.addingObjects(from: self.selections)

self.selection being an Array of

Thanks for your help.

Tim

1
  • You don't need to do it , because you can save an index with each Categories and later to sort them by the index. Commented Feb 18, 2020 at 3:43

1 Answer 1

3

Try the following instead of that line

if let categories = newQuestion.categories {
    newQuestion.categories = categories.addingObjects(from: self.selections)
} else {
    newQuestion.categories = Set(self.selections) as NSSet
}
Sign up to request clarification or add additional context in comments.

2 Comments

Works like a charm. I just needed to add: as NSSet but it works fine, Thank you. If I understood correctly the point of this is to extract the NSSet as a Set, into which we'll add the Array? if let categoriesToSAve = newQuestion.categories { newQuestion.categories = categoriesToSAve.addingObjects(from: self.selections) as NSSet } else { newQuestion.categories = Set(self.selections) as NSSet }
@Tim, not only, initially it might be nil, so it needs to create it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.