I am trying to inject the managedObjectContext in ViewModel and for some weird reason it is throwing a weird error.
struct ContentView: View {
@Environment(\.managedObjectContext) var viewContext
@StateObject var addBudgetVM: AddBudgetViewModel
init() {
// THIS LINE CAUSES ISSUES
addBudgetVM = AddBudgetViewModel(context: viewContext)
}
var body: some View {
// some code here
}
}
Cannot assign to property: 'addBudgetVM' is a get-only property
Here is the implementation of AddBudgetViewModel
import Foundation
import CoreData
class AddBudgetViewModel: ObservableObject {
@Published var name: String = ""
var context: NSManagedObjectContext
init(context: NSManagedObjectContext) {
self.context = context
}
func save() {
}
}