SWIFT: Why can't I get the current URL loaded in the UIWebView?

I need to load the current URL into webview, and here is how I try to get it, but it gives me this error: "Unable to convert exponent type" ST7 ?? "to enter" String "

and this is the code

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

What is wrong with this?

+7
url ios swift webview
source share
2 answers

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

 let currentURL : NSString = (webView.request?.URL.absoluteString)! 
+14
source share

Remember that your problem may not be just a syntax problem. If your webView is in a state where request == nil your application will crash at runtime.

I would rather write something like:

 if let currentURL = webView.request?.URL.absoluteString { // do things ... // Your currentURL will be automatically bridged to Swift String type } else { // Just in case request is nil ... } 
+3
source share

All Articles