Skip to content

Instantly share code, notes, and snippets.

@hartbit
Created July 23, 2019 15:50
Show Gist options
  • Save hartbit/8003810418002948317dc422d46ead83 to your computer and use it in GitHub Desktop.
Save hartbit/8003810418002948317dc422d46ead83 to your computer and use it in GitHub Desktop.
SwiftUI Crash
// To reproduce crash:
// 1. Navigate to second tab
// 2. Toggle the switch
// 3. Navigate to first tab
// Note: removing the NavigationView in the second tab avoids the crash
final class ViewModel: BindableObject {
var tab: Int = 0 { willSet { willChange.send(()) } }
var isOn: Bool = false { willSet { willChange.send(()) } }
let willChange = PassthroughSubject<Void, Never>()
}
struct RootView: View {
@EnvironmentObject var viewModel: ViewModel
var body: some View {
TabbedView(selection: $viewModel.tab) {
Text("One").tabItem { Text("One") }.tag(0)
LeafView().tabItem { Text("Two") }.tag(1)
}
}
}
struct LeafView: View {
@EnvironmentObject var viewModel: ViewModel
var body: some View {
// Comment NavigationView to resolve crash
NavigationView {
Toggle(isOn: $viewModel.isOn) {
Text("Toggle")
}
}
}
}
#if DEBUG
struct RootView_Previews: PreviewProvider {
static var previews: some View {
RootView().environmentObject(ViewModel())
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment