In SwiftUI, some solutions for a View must implement a ViewModifier or Protocol in LifeCyrcle. But in the View structure, it should be missing if another developer code forgot to be called. I want to implement it as a protocol; another developer must be called for that function in View LifeCycle. Have some solution to that?
struct ContentView: View {
var body: some View {
Text("Hello, World!")
.padding()
.onAppear {
// doSomething()
}
.mustCall { // Foo() } // I want it must be call.
}
}
struct MustImplement: ViewModifier {
var doSomething: () -> Void
func body(content: Content) -> some View {
ZStack(alignment: .bottomTrailing) {
content
.onAppear {
// doSomething()
}
}
}
}
extension View {
func mustCall(onCall: () -> Void) -> some View {
modifier(MustImplement(doSomething: onCall)
}
}