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'
我需要将当前的URL加载到webview中,这就是我试图得到它的方法,但它给了我这个错误:“无法转换exporessions类型'ST7 ??'输入'String'
and this is the code
这是代码
var currentURL : NSString = webView.request?.URL.absoluteString!
What is wrong with this?
这有什么问题?
2 个解决方案
#1
14
If you put parentheses around this, the error goes away:
如果你在括号周围加上括号,那么错误就会消失:
let currentURL : NSString = (webView.request?.URL.absoluteString)!
#2
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.
请注意,您的可能不仅仅是语法问题。如果您的webView处于request == nil状态,您的应用程序将在运行时崩溃。
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 ...
}
#1
14
If you put parentheses around this, the error goes away:
如果你在括号周围加上括号,那么错误就会消失:
let currentURL : NSString = (webView.request?.URL.absoluteString)!
#2
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.
请注意,您的可能不仅仅是语法问题。如果您的webView处于request == nil状态,您的应用程序将在运行时崩溃。
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 ...
}