I have a list of Menu
let muscleEtfs = Menu(name:"MuscleETFs", image:"image", destination: .muscleETFs)
let menus: [Menu] = [home, marketTrend, indexes, sectors, stockAnalysis, backtest, screener, myPortfolio, watchList, muscleStocks, muscleEtfs]
My goal is to change the destination of NavigationLink in SwiftUI conditionally. Could anybody explain to me why the switch case does not work but if does.
let muscleEtfs = Menu(name:"MuscleETFs", image:"image", destination: .muscleETFs)
var body: some View {
let menus: [Menu] = [home, marketTrend, indexes, sectors, stockAnalysis, backtest, screener, myPortfolio, watchList, muscleStocks, muscleEtfs]
return List {
ForEach(menus) { menu in
// This does not work
switch menu.destination {
case .news:
NavigationLink(
destination: HomeNewsView(menu: menu)
)
{
Text("\(menu.name)")
}
default:
NavigationLink(
destination: HomeNewsView(menu: menu)
)
{
Text("\(menu.name)")
}
}
// This does
if menu.destination == .news {
NavigationLink(
destination: HomeNewsView(menu: menu)
)
{
Text("\(menu.name)")
}
}
}
}
Using switch conditions throws compile error on Xcode
Closure containing control flow statement cannot be used with function builder 'ViewBuilder'