I'm currently working through Paul Hudson's 100 Days of SwiftUI bootcamp and I'm stuck at a challenge which asks to transfer objects from a struct (View) into a class (ViewModel) in order to create a MVVM-Architecture in the project. However when initializing a certain variable inside my struct initializer I get a runtime-warning So here are the initialized objects in my view struct:
@StateObject private var viewModel = EditViewViewModel()
@Environment(\.dismiss) var dismiss
var onSave: (Location) -> Void
initializer for struct:
init(location: Location, onSave: @escaping (Location) -> Void) {
self.onSave = onSave
viewModel.location = location // WARNING: Accessing StateObject's object without being installed on a View. This will create a new instance each time.
}
This is the variable that's causing problems and the initializer of my ViewModel:
var location: Location
init() {
self.location = Location(id: UUID(), name: "", description: "", latitude: 0.0, longitude: 0.0)
self.name = ""
self.description = ""
}