I had an issue with the default case where I wanted a "break" type situation for the switch to be exhaustive. SwiftUI requires some type of view, I found that
EmptyView()
solved the issue.
also noted here EmptyView Discussion that you "have to return something"
struct FigureListMenuItems: View {
var showContextType: ShowContextEnum
@Binding var isFavoriteSeries: Bool?
var body: some View {
Menu {
switch showContextType {
case .series:
Button(action: { toggleFavoriteSeries() }) {
isFavoriteSeries! ?
Label("Favorite Series?", systemImage: "star.fill")
.foregroundColor(.yellow)
:
Label("Favorite Series?", systemImage: "star")
.foregroundColor(.yellow)
}
default: // <-- can use EmptyView() in this default too if you want
Button(action: {}) {
Text("No menu items")
}
}
} label: {
switch showContextType {
case .series:
isFavoriteSeries! ?
Image(systemName: "star.fill")
.foregroundColor(.yellow)
:
Image(systemName: "star")
.foregroundColor(.yellow)
default:
EmptyView()
}
Label("Menu", systemImage: "ellipsis.circle")
}
}
private func toggleFavoriteSeries() {
isFavoriteSeries?.toggle()
}
}
Enum for switch
enum ShowContextEnum: Int {
case series = 1
case whatIsNew = 2
case allFigures = 3
}