SwiftyPredictor simplifies work with Yandex.Predictor service in iOS.
- Copy content of
Sourcefolder to your project.
or
- Use
SwiftyPredictorcocoapod.
- iOS 9.0 and later
- Xcode 8 and later
To initialize predictor, simply write something like this:
let predictor = Predictor(APIKey: "some_api_key")As you noticed, constructor requires API key. If you still don't have it, obtain new API key here (you'll need to authorize with Yandex account).
Now you can make requests for text suggestions:
predictor.requestSuggestions(forQuery: "how to ", inLanguage: .english, withLimit: 10) { (suggestions, error) in
for suggestion in suggestions {
print(suggestion.text)
}
if error != nil {
print("Error: \(error!)")
}
}The example above will print suggestions for phrase how to :
getmakeusebuydo
You can change language by its identifier or predefined name:
.english.russian.custom(identifier: "es")- Spanish language- etc.
If you want to receive full list of supported languages, use availableLanguages method:
predictor.availableLanguages { (languages, error) in
for language in languages {
print(language.identifier)
}
}All asynchronous requests made by Predictor instance are cancellable so you can stop them when it's needed:
/*
* Obtain reference to request instance.
*/
let request = predictor.requestSuggestions(forQuery: "how to ", inLanguage: .english, withLimit: 10) { (suggestions, error) in
// Do something with suggestions here...
}
/*
* Cancel request when needed.
*/
request.cancel()SwiftyPredictor is available under the MIT license. See the LICENSE file for more info.
