While talking to developers we get a lot of questions on how to initialize a @StateObject with external values.

Today we update our docs by greatly expanding around all our data flow primitives with special attention to StateObject. You'll find better explanations on how to initialize StateObject with external data and few words around what to pay attention when doing so (https://developer.apple.com/documentation/swiftui/stateobject)

We hope this helps everyone get more clarity and understanding on how to work with SwiftUI.

StateObject | Apple Developer Documentation

A property wrapper type that instantiates an observable object.

Apple Developer Documentation
@lucabernardi That is so much clearer now, and I love that it documents trickier use cases - please pass on my thanks to the writers! 🙌
@lucabernardi wonderful! Documentation is often a thankless task, but it has a huge impact on developer satisfaction. So thank you!
@lucabernardi thanks for that! We are introducing SwiftUI in a project now and I had some concerns about the best way to use @StateObject, that will be helpful.

@lucabernardi great article!
Just update the model with initializer, otherwise the MyInitializableView do not works:

class DataModel: ObservableObject {
@Published var name = "Some Name"
@Published var isEnabled = false

init(name: String = "Some Name", isEnabled: Bool = false) {
self.name = name
self.isEnabled = isEnabled
}
}

@lucabernardi Thanks for the heads up Luca! Code examples are always appreciated.
@lucabernardi This is great! Excited to see more focus on documentation 👏
@lucabernardi Thank you for adapting the documentation to real world scenarios.
@lucabernardi Amazing! Thanks, this is so helpful!
@lucabernardi this quality of documentation makes a big difference, thanks for putting in the work!
@lucabernardi This is so much better, thanks 🙏

@lucabernardi thank you so much for this! Glad to see that it reflects discussions I’ve soon on Ask Apple and here on Mastodon.

One thing that’s new to me is the autoclosure in the StateObject initializer with external value. I understand what it does for simple data types.

Does the autoclosure in initializer work the same way for richer and more stateful objects? I have to test my setup a bit and find out.

@lucabernardi I think the developers that asked these questions don't understand that @StateObject is a source of truth (the reference type equivalent of @State) so should not depend on any initialisation parameters. If it needs a parameter, it should simply be moved up in the hierarchy so its properties can be set. It is extremely likely these developers are misusing @StateObject for a legacy view model object instead of learning the View struct's features like diffing and dependency tracking.
@malhal @lucabernardi as someone who has asked this question in the past, I completely understand the point that @StateObject is like @State but for reference types now. I think it’s worth a discussion and how the alternatives may be used, especially for those looking into injecting values into the view model without having to rely on optionals.