2016-01-31 35 views
0

我遇到一个小问题,将我的Core Data对象附加到数组。所有内容都正确保存在Core Data中,但是当我检查数组时,它是空的。代码看起来如下:核心数据对象不附加到数组

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") 
    } 
} 

什么似乎工作对我来说是改变我的数组从类型卡,以键入字符串。然后将newQA.question和newQA.answer分别附加到playerCards中。虽然我不确定这是一个有效的解决方案。由于我不确定这个问题,答案会保持这种联系。任何帮助是伟大的,先谢谢你。

+0

您可以检查数据库(下载应用程序包和检查表)? – Mohamad

+0

感谢您的回复,我确实这样做了,我检查了我的sqlite文件以确保所有内容都正确保存。它只是不附加到数组。 – ACerts

+0

对不起,你确定你的playerCards!= nil? – Mohamad

回答

1

到您的NSMutableArray这样你应该添加的数据:

playerCards.addObject(newQA)