Created
June 1, 2022 15:13
-
-
Save NikolajMosbaek/f4f493469589dc061de1cef581e146de to your computer and use it in GitHub Desktop.
With this code you can make an object Searchable and define which parameters that should be included in a search. Then you can easily call .fullyFilter(for: someString) on the object
This file contains hidden or 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
protocol Searchable { | |
var keyPaths: [KeyPath<Self, String>] { get } | |
} | |
extension Searchable { | |
func matches(_ searchString: String) -> Bool { | |
for path in keyPaths { | |
let value = self[keyPath: path] | |
if value.localizedCaseInsensitiveContains(searchString) { | |
return true | |
} | |
} | |
return false | |
} | |
} | |
extension Sequence where Element: Searchable { | |
func fuzzyFilter(for searchString: String) -> [Element] { | |
filter { $0.matches(searchString) } | |
} | |
} |
Example:
struct Bus: Decodable, Identifiable, Searchable {
enum CodingKeys: CodingKey {
case id, name, location, destination
}
var id: Int
var name: String
var location: String
var destination: String
var keyPaths: [KeyPath<Bus, String>] = [\.name, \.location, \.destination]
}
struct ContentView: View {
@State private var buses = [Bus]()
@State private var searchText = ""
var filteredBuses: [Bus] {
if searchText.isEmpty {
return buses
} else {
return buses.fuzzyFilter(for: searchText)
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From HWS+ https://www.hackingwithswift.com/plus/advanced-swift/runtime-reflection-with-mirror