UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SearchBar for SwiftUI 2.0

Forums > 100 Days of SwiftUI

How do I make a Searchbar on text in Xcode 12.2 beta 2, without reverting to the complicated Swift code? The code below compiles and successfully displays all the animals, but the second part of the terneray operator does not filter a search of any kind. Much obliged,

Bruckner

AnimalModel.swift:

import SwiftUI

struct Animal: Codable, Identifiable, Hashable {
    let id: String
    let name: String
    let headline: String
    let description: String
    let link: String
    let image: String
    let gallery: [String]
    let fact: [String]
}

ContentView.swift:

import SwiftUI

struct ContentView: View {
    // MARK: - PROPERTIES
    @State private var searchText: String = ""
    //@State private var text: String = ""
    let animals: [Animal] = Bundle.main.decode("animals.json")

    //@ObservedObject var searchBar: SearchBar = SearchBar()

    // MARK: - BODY

    var body: some View {
        NavigationView {
            ScrollView {
                HStack {
                    TextField("Search animals here", text: $searchText)
                }
                .padding()
                .background(Color(.systemGray5))
                .cornerRadius(8)
                .padding(.horizontal)

                ForEach(animals.filter({_ in 
                                        self.searchText.isEmpty ? true :
                                            "$0".lowercased().contains(searchText.lowercased())})
                )  {
                    animal in
                    HStack {
                        Image(animal.image)
                            .resizable()
                            .scaledToFit()
                            .frame(width: 80, height: 80)

                        Text(animal.name)
                            .font(.headline)
                            .fontWeight(.bold)
                        Spacer()
                    }//: HSTACK
                    .padding()
                    Divider()
                }//: LOOP
            }
            .navigationBarTitle("African Animals")
        }
    }
}

3      

hi Bruckner,

you wrote "$0".lowercased() in the tail of the ternary. did you mean $0.lowercased() ?

hope that helps,

DMG

3      

If we remove the quotes, we get the error Anonymous closure arguments cannot be used inside a closure that has explicit arguments. I'm also looking now (March 2021) for a nice code for the SwiftUI 2.0 search bar with a magnifying glass picture, but still haven't found any nice working way.

4      

While it not the in the title but the code shows a search bar, from WWDC20

Embrace Swift type inference

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.