简体   繁体   中英

Core data object not appending to array

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.

您应该像这样向您的NSMutableArray添加数据:

playerCards.addObject(newQA)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM