I am having a small issue with appending my Core Data object to an array. Everything is being saved correctly in Core Data, but when I checked the array, it was empty. The code looks as follows:
func saveQA(question: String, answer: String) {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("Card", inManagedObjectContext: managedContext)
let newQA = Card(entity: entity!, insertIntoManagedObjectContext: managedContext)
newQA.question = question
newQA.answer = answer
do {
try managedContext.save()
playerCards.append(newQA)
}
catch {
print("error")
}
}
What seemed to work for me was changing my array from type Card, to type String. Then appending both newQA.question and newQA.answer separately to playerCards. Although I am unsure this is a valid solution. As I am unsure the question and answer will stay linked to each other that way. Any help is great, thank you in advance.