Как насчёт того, чтобы сделать мир проще, как показано ниже ...
enum SectionPane: Equatable, Identifiable, CustomStringConvertible {
case servers
case snippets
var description: String {
switch self {
case .servers:
return "server"
case .snippets:
return "snippet"
}
}
var id: SectionPane { self }
}
class SectionPaneViewModel: ObservableObject {
@Published var highlightedPane: SectionPane? = nil
@Published var selectedPane: SectionPane? = nil
}
struct TestEnumPublisher: View {
@ObservedObject var vm = SectionPaneViewModel()
@State private var active: String = ""
var body: some View {
NavigationView {
VStack(alignment: .leading) {
// Current selection label
Text("Selected: \(active)")
NavigationLink(destination: Text("first view").onAppear{ self.vm.highlightedPane = .servers },
tag: SectionPane.servers, selection: $vm.selectedPane) {
Text("First")
}
NavigationLink(destination: Text("second view").onAppear{ self.vm.highlightedPane = .snippets },
tag: SectionPane.snippets, selection: $vm.selectedPane) {
Text("Second")
}
Spacer()
}.onReceive(vm.$selectedPane, perform: { selection in
self.active = selection?.description ?? "none"
})
}
}
}