iOSおよびSWIFTラーニング-CoreData->CRUDにおけるCore Data付きHow to Save Dataの作成
( ToDo App example )
let newItem = Item()
let context = (UIApplication.shared.delegate as! AppDelegate),persistentContainer.viewContext
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newItem = Item(context: context)
In our saveItems( ) method, what we ultimately need to do is we need to be able to "commit"our context to permanent storage inside our persistentContainer.
In order to do that, we need to call "try context.save( )"
→ This basically transfers what's currently inside our staging area, the context, to our permanent data stores.
func saveItems() {
do {
try context.save()
} catch {
print("Error saving context \(error)")
}
self.tableView.reloadData()
}
@IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
var textField = UITextField()
let alert = UIAlertController(title: "Add New ToDo Item", message: "", preferredStyle: .alert)
// The button you are going to press once you are done
let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
// What will happen once the user clicks the Add Item button on our UIAlert
let newItem = Item(context: self.context)
newItem.title = textField.text!
newItem.isDone = false // must give value if not set to optional
self.itemArray.append(newItem)
self.saveItems()
}
alert.addTextField { (alertTextField) in
alertTextField.placeholder = "Create new item"
textField = alertTextField // Storing the same reference to a local variable accessible
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
Reference
この問題について(iOSおよびSWIFTラーニング-CoreData->CRUDにおけるCore Data付きHow to Save Dataの作成), 我々は、より多くの情報をここで見つけました https://velog.io/@kevinkim2586/iOS-Swift-공부-CoreData-How-to-Save-Data-with-Core-Data-Create-in-CRUDテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol