Search code examples
xcodetypesswift3

How to concatenate unwrapped optional and string


Executing the code below should open a new URL at baseURL + path. However, it results in the following error:

error:Argument label String do not match any available overloads in let url = URL(String:urlString)

The baseURL and path variables come from a JSON file. baseURL is a string and path is an unwrapped optional.

 override func viewDidLoad() {
    super.viewDidLoad()
    let newPath = path! as! String
    let urlString = baseURL + newPath
    let url = URL(String:urlString)
    let requestObj = URLRequest(URL: url)
    webView.loadRequest(requestObj)
}

How can I resolve this issue?


Solution

  • Assuming baseURL and path are of type String

    let baseURL = "http://10.150.160.170"
    let path = "/welcome-page"
    let urlString = baseURL + path
    let url = URL(string: urlString)
    let requestObj = URLRequest(url: url!)