Skip to content

Instantly share code, notes, and snippets.

@iosdevie
Created June 9, 2021 09:54
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 iosdevie/66e5797931b909b53c540873e1960769 to your computer and use it in GitHub Desktop.
Save iosdevie/66e5797931b909b53c540873e1960769 to your computer and use it in GitHub Desktop.
import SwiftUI
struct Colors : Identifiable{
var id = UUID()
var name : String
}
struct SearchingLists: View {
@State private var searchQuery: String = ""
@State private var colors: [Colors] = [Colors(name: "blue"),
Colors(name: "Red"),
Colors(name: "Green"),
Colors(name: "Yellow"),
Colors(name: "Pink"),
Colors(name: "Purple"),
Colors(name: "White"),
Colors(name: "Orange"),
Colors(name: "Black"),]
var body: some View {
NavigationView {
List {
ForEach (searchResults, id:\.id){ color in
Text(color.name)
}
}
.searchable("Search color", text: $searchQuery,
placement: .automatic)
.navigationTitle("Colors List")
}
}
var searchResults: [Colors] {
if searchQuery.isEmpty{
return colors
}
else{
return colors.filter {$0.name.lowercased().contains(searchQuery.lowercased())}
}
}
}
struct SearchingLists_Previews: PreviewProvider {
static var previews: some View {
SearchingLists()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment