Wednesday, September 10, 2025
HomeiOS Developmentios - Can I exploit .searchScopes with an non-compulsory binding?

ios – Can I exploit .searchScopes with an non-compulsory binding?


For my app I am making a search view. Looking will be completed in a number of scopes with the accessible scopes being dynamic. When loading the search view the scopes are fetched utilizing an API name, however to take action the chosen scopes is an non-compulsory. It’s unknown which scopes can be found so there is not a particular scope till the fetch is completed (the primary scope is chosen by default). Having an non-compulsory chosen scope would not work, after I make it non-optional by onerous coding a price it does work. Is there a approach to get .searchSopes working with an non-compulsory choice or is there a approach to have the choice not be non-compulsory when the search scopes are loaded?

struct SearchView: View {
    @State personal var searchText = ""
    @State personal var searchScopes = [SearchScope]()
    @State personal var searchScope: SearchScope?

    var physique: some View {
        NavigationStack {
            Textual content("Trying to find (searchText)")
        }
        .searchable(textual content: $searchText)
        .searchScopes($searchScope, activation: .onSearchPresentation, {
            ForEach(searchScopes) { scope in
                Textual content(scope.label).tag(scope)
            }
        })
        .activity {
            self.searchScopes = await httpClient.fetchSearchScopes()
            if !searchScopes.isEmpty {
                self.searchScope = searchScopes[0]
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments