I’ve a query a few search operate, particularly learn how to get the phrase to look on one view and to show the outcome on one other sheet with swift. If the search bar and the place to show the outcome are the identical, the search operate works effectively, so I really feel like viewModel.listings can’t be up to date correctly with updateListingsForLocation(). That may be appreciated if you happen to might assist.
Search Outcomes
Search Bar
# A View with a Search bar
@ObservedObject var viewModel = SearchedWordsObject(service: ExploreService())
TextField("Search phrases", textual content: $viewModel.searchWord)
.onChange(of: viewModel.searchWord) {
if viewModel.searchWord.isEmpty {
searchWord = false
viewModel.updateListingsForLocation()
} }
.onSubmit {
viewModel.updateListingsForLocation()
searchWord = true
showCreateView = true }
.sheet(isPresented: $showCreateView) {
SearchView(lookForWord : $searchWord)
}
# Sheet
@StateObject var viewModel = SearchedWordsObject(service: ExploreService())
@Binding var lookForWord : Bool
if lookForWord {
LazyVStack(spacing: 10) {
ForEach(viewModel.listings) { itemizing in
HStack {
Textual content("(itemizing.wordID)")
Textual content("(itemizing.phrase) : (itemizing.meaning1)")
# Information
class SearchedWordsObject : ObservableObject {
@Revealed var listings = [Word] ()
@Revealed var searchWord = ""
non-public var listingsCopy = [Word]()
non-public let service : ExploreService
func updateListingsForLocation() {
if searchWord.isEmpty { self.listings = self.listingsCopy }
else {
let filteredListings = listings.filter({
$0.phrase.lowercased() == searchWord.lowercased()})
self.listings = filteredListings
}}}
I adopted the youtube tutorial