0

I need to create GET request like this:

https://public-api.nazk.gov.ua/v1/declaration/?q=Чер

https://public-api.nazk.gov.ua/v1/declaration/?q=Володимирович

Last characters after = are Cyrillic symbols

    let hostURL = "https://public-api.nazk.gov.ua/v1/declaration/?q="
    let url = URL(string: (hostURL + searchBar.text!))!

I have a run time error because it is impossible to force unwrap Cyrillic symbols. How can I avoid this issue?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ioann Will
  • 83
  • 10
  • Instead of completely changing your question, please post a new question specific to your new issue about decoding the obtained data. – rmaddy Jul 23 '18 at 14:30

1 Answers1

0

Try using this.

let hostURL = "https://public-api.nazk.gov.ua/v1/declaration/?q="
hostURL = hostURL + searchBar.text!

let encodedUrl = hostURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

let url = URL(string:encodedUrl)
aBilal17
  • 2,974
  • 2
  • 17
  • 23