Skip to content

Instantly share code, notes, and snippets.

@DamianMehers
Created January 15, 2024 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DamianMehers/9a542ad5e1fafd9547ca23b099369c24 to your computer and use it in GitHub Desktop.
Save DamianMehers/9a542ad5e1fafd9547ca23b099369c24 to your computer and use it in GitHub Desktop.
macOS SwiftUI crash with .searchable bound to ViewModel and sidebar placement
//
// TestSearchStringInViewModelApp.swift
// TestSearchStringInViewModel
//
// Created by Damian Mehers on 2024-01-15.
// https://damian.fyi/
//
// Run this app on MacOS (all source here) ... tap "a"
// in search bar and then delete the text ... crash
import SwiftUI
@main
struct TestSearchStringInViewModelApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@Bindable var viewModel = ViewModel()
var body: some View {
NavigationSplitView {
VStack {
List(1..<5) { n in
Text("\(n)")
}
.listStyle(SidebarListStyle())
// Works if not sidebar or @State searchString
.searchable(text: $viewModel.searchString, placement: .sidebar)
}
} detail: {
Text("Some detail")
}
}
}
@Observable final class ViewModel {
var searchString = ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment