Your theUrlString
is not properly encoded. As a result, when you use URL(string:)
, it is returning nil
(an indication that the URL string passed in was malformed).
I would recommend using URLComponents
to create your URL.
Something like:
var urlComponents = URLComponents(string: "http://www.ksl.com/auto/search/index")
var arguments: [String: String] = [
"keyword": "",
"make": "Chevrolet",
"model": "Silverado 1500"
]
var queryItems = [URLQueryItem]()
for (key, value) in arguments {
queryItems.append(URLQueryItem(name: key, value: value))
}
urlComponents?.queryItems = queryItems
if let url = urlComponents?.url {
print(url) // http://www.ksl.com/auto/search/index?keyword=&model=Silverado%201500&make=Chevrolet
}
URLComponents
API Reference: https://developer.apple.com/reference/foundation/urlcomponents