Search code examples
iosswifturlalamofire

How to replace placeholder letter in URL?


I have the URL to there I send my requests to get the results:

let URL = "https://api.myweb.site/search/\(searchText)"

but when my searchText contains a placeholder letter my app crashes. By the way, I'm using Alamofire.

How can I make my URL safe and avoid the crash? I've tried this:

let escapedString = searchText.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!

but it crashes again. What is the solution for my problem?


Solution

  • The solution is:

    let newURL:String = oldURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!