I want to return a View depending on content of an field in a array. This works, but i got a problem if i try to add ViewModifiers to the View. This won't works.
struct CodesView: View {
var body: some View {
ScrollView{
ForEach(0 ..< codes.count) {idx in
Result(selector: self.codes[idx].Zeile)
}
}
}
}
struct Result: View{
var selector: String
var body: some View{
switch selector {
case "->Text1":
return VStack{
Text("Lorem ipsum dolor sit amet")
.bold()
}
// serval other case will follow...
default:
return VStack{
Text("\(selector)")
.frame(width: 350, alignment: .leading) // line throw an error
.font(.system(.body, design: .monospaced)) // line throw an error
}
}
}
}
Errormessage is: Function declares an opaque return type, but the return statements in its body do not have matching underlying types
How do I need to declare the function to get the result with ViewModifier? I need different layout for the different returned Views.
The following solves your case
struct Result: View {
var selector: String
var body: some View {
Group {
if self.selector == "->Text1" {
VStack {
Text("Lorem ipsum dolor sit amet")
.bold()
}
} else {
VStack {
Text("\(selector)")
.frame(width: 350, alignment: .leading)
.font(.system(.body, design: .monospaced))
}
}
}
}
}