Skip to content

Instantly share code, notes, and snippets.

@SmartJSONEditor
Last active September 22, 2023 15:27
Show Gist options
  • Save SmartJSONEditor/33d4791416e7b47666c12c3cde4ca96e to your computer and use it in GitHub Desktop.
Save SmartJSONEditor/33d4791416e7b47666c12c3cde4ca96e to your computer and use it in GitHub Desktop.
class ViewModel: ObservableObject {
@Published public var title: String
deinit {
print("deinit ViewModel")
}
init(title: String) {
self.title = title
print("init ViewModel")
}
}
struct ContentView: View {
/// View Owns object
@StateObject var viewModel: ViewModel
/// Passing some injected values
init(title: String) {
_viewModel = StateObject(wrappedValue: ViewModel(title:title))
}
var body: some View {
VStack {
Text(viewModel.title)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment