Created
July 16, 2017 20:01
-
-
Save ncreated/22bbc5c8e912d9acbe9d0a4e860be13c to your computer and use it in GitHub Desktop.
Medium blogpost snippet
This file contains 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
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