I’m trying to add data to my model “Word” and when I push the button which connected with function “addWord”. After that I got the error
Fatal error: failed to find a currently active container for Word
Here is my code
Model:
import SwiftData
@Model
class Word {
var title: String
var response: String
var translate: String
init(title: String, response: String, translate: String) {
self.title = title
self.response = response
self.translate = translate
}
}
ModelContainer:
import SwiftUI
import SwiftData
@main
struct GPTApp: App {
var body: some Scene {
WindowGroup {
ContentViev()
}
.modelContainer(for: Word.self)
}
}
How I add my data and I don’t forget to put model context:
struct ContentViev: View {
@Environment (.modelContext) var modelContext
func addword(){
let word = Word(title: "123", response: "123", translate: "123")
modelContext.insert(word)
}