I was trying to write a static function like this:
enum NavigationViewKind {
case test1(String)
case test2
}
struct NavigationViewManager {
static func make<V: View>(_ kind: NavigationViewKind, _ contentView: @escaping () -> V) -> some View {
switch kind {
case .test1(let text):
return NavigationView {
contentView()
.navigationBarTitle(text, displayMode: .inline)
}
case .test2:
return NavigationView {
contentView()
.navigationBarTitle("Initial", displayMode: .large)
}
}
}
}
But I'm getting this error :
Function declares an opaque return type, but the return statements in its body do not have matching underlying types
Can you please help me understanding and solving this error?
Thank you
You need to use @ViewBuilder
in this case (and remove returns, because return disables ViewBuilder)
struct NavigationViewManager {
@ViewBuilder
static func make<V: View>(_ kind: NavigationViewKind, _ contentView: @escaping () -> V) -> some View {
switch kind {
case .test1(let text):
NavigationView {
contentView()
.navigationBarTitle(text, displayMode: .inline)
}
case .test2:
NavigationView {
contentView()
.navigationBarTitle("Initial", displayMode: .large)
}
}
}
}