0

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.

5
  • can you check the data base ( download app bundle and check your table) ? Commented Jan 31, 2016 at 6:04
  • Thank you for the reply, I did do that, i checked my sqlite file to make sure everything was saved correctly which it was. It is just not appending to the array. Commented Jan 31, 2016 at 6:06
  • i'm sorry , but are you sure your playerCards != nil ? Commented Jan 31, 2016 at 6:08
  • I am not sure if i am misunderstaning. That is what my issue is. playerCards is my array which is staying empty after i pass in arguments for question and answer. Everything is being saved correctly to core data, but not being appended to the array. Commented Jan 31, 2016 at 6:12
  • if your array is nil and you didn't alloc and init , it so you cant't append data to it ! Commented Jan 31, 2016 at 6:14

1 Answer 1

1

you should add data to you NSMutableArray like this :

playerCards.addObject(newQA)
Sign up to request clarification or add additional context in comments.

Comments

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.