CoreData 不将数据保存到数据库

CoreData doesn't save data to database

我正在按照 Youtube 上的教程使用 Swift 编写 iOS 应用程序。该应用程序将具有与待办事项列表应用程序相同的功能,但用途不同。但是,当我希望保存数据(并在调试器中打印)时,什么也没有发生。我做错了什么吗?

 @IBAction func saveTapped(sender: AnyObject) {
    println("SaveTapped")

    // Reference to our app delegate

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

    // Reference moc

    let contxt: NSManagedObjectContext = appDel.managedObjectContext!
    let en = NSEntityDescription.entityForName("Tankningslista", inManagedObjectContext: contxt)

    // Create instance of our data model and initialize

    var nyTankning = Model(entity: en!, insertIntoManagedObjectContext: contxt)

    // Map our properties

    nyTankning.liter = (textFieldLiter.text as NSString).floatValue
    nyTankning.kronor = (textFieldKronor.text as NSString).floatValue
    nyTankning.literpris = (textFieldLiterpris.text as NSString).floatValue
    //nyTankning.datum = datePickerDatum.date

    // Save our context

    contxt.save(nil)

    println(nyTankning) //HERE I ESPECT THE DATA TO BE PRINTED IN THE DEBUG WINDOW

    // navigate back to root vc
    self.navigationController?.popToRootViewControllerAnimated(true)


}

您需要先获取数据才能显示。现在你只保存它。保存数据后查看我在代码中添加的内容。

@IBAction func saveTapped(sender: AnyObject) { println("SaveTapped")

// Reference to our app delegate

let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

// Reference moc

let contxt: NSManagedObjectContext = appDel.managedObjectContext!
let en = NSEntityDescription.entityForName("Tankningslista",inIntoManagedObjectContext: contxt)

// Create instance of our data model and initialize
// Use your NSManagedModel to initialize not only the Model Keyword!
var nyTankning = Tankningslista(entity: en!, insertIntoManagedObjectContext: contxt)

// Map our properties

nyTankning.liter = (textFieldLiter.text as NSString).floatValue
nyTankning.kronor = (textFieldKronor.text as NSString).floatValue
nyTankning.literpris = (textFieldLiterpris.text as NSString).floatValue
//nyTankning.datum = datePickerDatum.date

// Save our context

 if contxt.save(nil) {

// Fetch the Data from Core Data first....

let fetchRequest = NSfetchRequest(entityName: "Tankningslista")
var error:NSError?

var result = contxt.executeFetchRequest(fetchRequest, error: &error) as [Tankningslista]

    for res in result {
     // Now you can display the data
        println(res.liter)
        ......
        ......
     }

// End of the fetching

} else {

println(error)    

}

//println(nyTankning) //HERE I ESPECT THE DATA TO BE PRINTED IN THE DEBUG WINDOW

// navigate back to root vc
self.navigationController?.popToRootViewControllerAnimated(true)

希望该解决方案能解决您的问题。

嗯,还有很多需要改进的地方,我真的建议你在 Udemy/ Bitfountain 或 Udacity 这样的平台上学习 "real" iOS/ swift 课程。首先,您需要在函数中的某处获取 ManagedObject(然后将结果存储在数组中)或使用 NSFetchedResultController(主要用于带有 TableViews 的 CoreData)然后我不确定您的 numberOfRowsInSection 函数中有什么,在这里也应该有正确的 return 值。正如我所说,在这里解决这个问题真的太多了......