【问题标题】:SWIFT: Why I can't get the current URL loaded in UIWebView? [closed]SWIFT:为什么我无法在 UIWebView 中加载当前 URL? [关闭]
【发布时间】:2014-12-19 00:13:01
【问题描述】:

我需要将当前 URL 加载到 webview 中,这就是我试图获取它的方式,但它给了我这个错误:“无法转换 exporessions 类型 'ST7??'输入“字符串”

这是代码

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

这有什么问题?

【问题讨论】:

    标签: ios url swift webview


    【解决方案1】:

    如果你把括号括起来,错误就会消失:

    let currentURL : NSString = (webView.request?.URL.absoluteString)!
    

    【讨论】:

    • SWIFT 3 -- 让 currentURL = self.webView.request?.url?.absoluteString
    【解决方案2】:

    请注意,您的问题可能不仅仅是语法问题。 如果您的 webView 处于 request == nil 您的应用程序将在运行时崩溃的状态。

    我宁愿写这样的东西:

    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 ...
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多