8

I need to get the current URL loaded into the webview and this is how I'm trying to get that but it gives me this error: "Cannot convert the exporessions type 'ST7??' to type 'String'

and this is the code

 var currentURL : NSString = webView.request?.URL.absoluteString!

What is wrong with this?

ernestocattaneo
  • 1,197
  • 5
  • 18
  • 30

2 Answers2

14

If you put parentheses around this, the error goes away:

let currentURL : NSString = (webView.request?.URL.absoluteString)!
Rob
  • 415,655
  • 72
  • 787
  • 1,044
3

Beware that yours might not be just a syntax problem. If your webView is in a state where request == nil your app will crash at runtime.

I'd rather write something like:

if let currentURL = webView.request?.URL.absoluteString {

    // do things ...
    // Your currentURL will be automatically bridged to Swift's String type 

} else {

   // Just in case request is nil ...

}
Matteo Piombo
  • 6,688
  • 2
  • 25
  • 25