加载时 UIWebView 崩溃 URL

UIWebView crashing when loading URL

这应该非常简单,但我不知道为什么这段代码不起作用。我尝试像这样将基本 URL 加载到 UIWebView 中:

    let string = "www.google.com".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
    let url = NSURL(string: string!)

    if let ur = url {
        let request = NSURLRequest(URL: ur)
        webView.loadRequest(request)
    }

然而每次我收到这个错误:

致命错误:在展开可选值时意外发现 nil

单步执行,什么都没有 nil。我想我会这样做只是为了确保:

    let string = "www.google.com".stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

    // unwrap the string
    if let str = string {

        let url = NSURL(string: str)

        // unwrap the url
        if let ur = url {
            let request = NSURLRequest(URL: ur)
            webView.loadRequest(request)
        }
    }

我仍然遇到同样的崩溃!如果我查看线程,我会在设备上 运行 时得到这个:

0x656d6c <+68>: bl 0x6abac4 ; function signature specialization 1 = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)


所以我的 UIWebViewnil 不知何故...我的一切都正确连接了吗?有任何想法吗

WebView 本身必须是 nil。如果出口左边有一个空圆圈,从视图拖放到出口重新连接。

@penatheboss 的回答是正确的,但我想 post 我是如何修复它的。我的 IBOutlets 连接正确,但是我的 UIWebView 在我的 class 中总是 nil。即使在 viewDidAppear

我是如何修复它的:

  • 清理了我的项目
  • 相当Xcode
  • 已删除派生数据文件夹
  • 重新打开项目构建

然后它与我 posted 的原始代码一起工作得很好。 说话。关于。烦人。