Skip to content

Instantly share code, notes, and snippets.

@ncreated
Created July 16, 2017 20:01
Show Gist options
  • Save ncreated/22bbc5c8e912d9acbe9d0a4e860be13c to your computer and use it in GitHub Desktop.
Save ncreated/22bbc5c8e912d9acbe9d0a4e860be13c to your computer and use it in GitHub Desktop.
Medium blogpost snippet
import RxSwift
private let countries = ["Afghanistan", "Albania", /* all the countries */ "Zambia", "Zimbabwe"]
final class CountryAutocompleteProvider: AutocompleteProvider {
func autocomplete(text: String) -> Observable<[Prediction]> {
func predictionsFor(prefix: String) -> [Prediction] {
return countries
.flatMap { country in
if let matchingRange = country.range(of: prefix, options: .caseInsensitive) {
return Prediction(predictedText: country, matchingRange: matchingRange)
} else {
return nil
}
}
}
return Observable.just(text)
.map { predictionsFor(prefix: $0) }
.delay(0.3, scheduler: MainScheduler.instance) // 0.3 second delay to simulate network
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment