Recently, I’ve started to learn Swift with Playground app on my iPad.
So, I’m trying to make a picker button who changes the text on the page when it’s pressed.
But I’ve “Unexpected code in closure” error on line 7.
Here is my code:
import SwiftUI
struct HomeView: View {
@State var selected = 0
var timeline: some View {
VStack (alignment: .leading) {
is $selected == 0 {
Text("Test 1")
} else {
Text("Test")
}
}.padding()
}
var body: some View {
NavigationStack {
ScrollView(.vertical, showsIndicators: false) {
VStack {
Text("Timeline")
.font(.headline)
} .padding()
Picker(selection: $selected, label: Text(""), content: {
Text("Today").tag(0)
Text("Recommandation").tag(1)
}).pickerStyle(SegmentedPickerStyle())
.padding()
timeline
}
}
}
}
I’ve tried to change it by writing
is $selected == 0 {
return AnyView(Text("Test 1"))
} else {
return AnyView(Text("Test"))
}
but it doesn’t correct the error.
New contributor
Jean-Chichi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.