Created
January 15, 2024 13:28
-
-
Save DamianMehers/9a542ad5e1fafd9547ca23b099369c24 to your computer and use it in GitHub Desktop.
macOS SwiftUI crash with .searchable bound to ViewModel and sidebar placement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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