I try to implement Search as you type written in Swift. It works already but I need some tuning. I send with each letter typed in
func searchBar(searchBar: UISe
The only thing u need is to create a Timer
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let text = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
// Create a Timer in this or parent class.
debounceTimer.invalidate() // just in case this button is tapped multiple times
// start the timer
debounceTimer = Timer.scheduledTimer(timeInterval: 0.8, target: self, selector: #selector(self.startSearch(_:)), userInfo: text, repeats: false)
}
func startSearch(_ timer: Timer) {
if let searchText = timer.userInfo as? String {
print("Searching: \(searchText)")
// make your service call here ...
}
}