I'm creating a new
xcdatamodeld
file and create an entity called ProgrammingLanguage that has two string attributes: “name” and “creator”.then, using it here to create new objects:
import SwiftUI
struct ContentView: View {
// Anywhere you need to create Core Data objects you should add an @Environment property to ContentView to read the managed object context right out:
@Environment(\.managedObjectContext) var managedObjectContext
var body: some View {
Button(action: {
let language = ProgrammingLanguage(context: self.managedObjectContext)
language.name = "Python"
language.creator = "Guido van Rossum"
// See whether your managed object context has any changes
if self.managedObjectContext.hasChanges {
// Save the context whenever is appropriate
do {
try self.managedObjectContext.save()
} catch {
// handle the Core Data error
}
}
}) {
Text("Insert example language")
}
}
}
How can I modify that object once created?
Thank you