What is the equivalent of UIKit View's deinit for a SwiftUI view?
I have looked at .onDisappear... but, it does not seem to me that is the hook I should be using to know when a SwiftUI View is gone for good, for example during navigation etc...
There is no deinit for structs, however, you can use some of the workarounds:
@StateObject, you can use deinit of the class, e.g.:struct YourView: View {
@StateObject var viewModel = ViewModel()
}
class ViewModel: ObservableObject {
init() {}
deinit { /* When the view is destroyed
the initialized ViewModel will be deinited as well */ }
}
deinit right before dismiss():struct SheetContents: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
Button("Done") {
/* "Deinit" code here. */
dismiss()
}
}
}
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