struct ProfileEditView: View {
@ObservedObject var viewModel: UsersViewModel
@StateObject var auth: Authenticator
@State var showingImageEditor: Bool = false
init(_ viewModel: UsersViewModel, _ auth: Authenticator) {
self.viewModel = viewModel
self.auth = auth
UITableView.appearance().backgroundColor = UIColor.clear
UITableViewCell.appearance().selectionStyle = .none
}
var body: some View { }
}
I am trying to manually intialize a view that takes a StateObject
as a parameter. I am getting an error: Cannot assign to property: 'auth' is a get-only property
. What is the proper way to write the initializer?
@StateObject var model = DataModel() SwiftUI creates a new instance of the object only once for each instance of the structure that declares the object. When published properties of the observable object change, SwiftUI updates the parts of any view that depend on those properties: Text(model.
Instead, declare a property with the @StateObject attribute in a View, App, or Scene, and provide an initial value. Apple tries to optimize a lot under the hood, don't fight the system. Just create an ObservableObject with a Published value for the parameter you wanted to use in the first place.
That is because the @StateObject gets initialized after the View.init () and slightly before/after the body gets called. I tried a lot of different approaches on how to pass data from one view to another and came up with a solution that fits for simple and complex views / view models.
The answer given by @Asperi should be avoided Apple says so in their documentation for StateObject. You don’t call this initializer directly. Instead, declare a property with the @StateObject attribute in a View, App, or Scene, and provide an initial value.
So we can initialize it without passing parameters. greeting = "Hello, \ (name)!" <1> We update our initializer to accept an optional string with a default value of nil.
I can't fully reproduce your code without your definitions of Authenticator
and UsersViewModel
, but I got it to compile:
class UsersViewModel: ObservableObject {}
class Authenticator: ObservableObject {}
struct ProfileEditView: View {
@ObservedObject var viewModel: UsersViewModel
@StateObject var auth: Authenticator
@State var showingImageEditor: Bool = false
init(_ viewModel: ObservedObject<UsersViewModel>, _ auth: Authenticator) {
_viewModel = viewModel
_auth = StateObject(wrappedValue: auth)
UITableView.appearance().backgroundColor = UIColor.clear
UITableViewCell.appearance().selectionStyle = .none
}
var body: some View {
Text("something")
}
}
These are the key changes:
init(_ viewModel: ObservedObject<UsersViewModel>, _ auth: Authenticator) {
_viewModel = viewModel
_auth = StateObject(wrappedValue: auth)
If you don't understand my changes you should google
"swift property wrappers"
to get a better understanding of what property wrappers are and how to use them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With