使用SFSafariViewController时找到nil

时间:2018-03-08 17:53:42

标签: swift xcode

在这个视图中,我正在编写一个包含一些变量的URL,并通过SFSafariVeiwController打开它。我收到这个错误:

  

“线程1:致命错误:在展开时出乎意料地发现nil   可选值“。

使用的代码如下:

@IBAction func pay(_ sender: Any) {

    let urlString:String = "https://website.com/video.php?user=\(user)&pass=\(pass)&texto=\(texto)&esp=\(espec)&l_origen=\(l_origen)&l_destino=\(l_destino)"


    let svc = SFSafariViewController(url: NSURL(string: urlString)! as URL)
        self.present(svc, animated: true, completion: nil)
}

感谢您的时间

用Amey的答案编辑:

@IBAction func pay(_ sender: Any) {

        let urlString:String = "https://website/video.php?user=\(user)&pass=\(pass)&texto=\(texto)&esp=\(espec)&l_origen=\(l_origen)&l_destino=\(l_destino)"

        if let tempString = urlString {
            if let url = URL(string:tempString){
                let svc = SFSafariViewController(url: url)
                self.present(svc, animated: true, completion: nil)
            }
        }
    }

但是,我得到“条件绑定的初始化器必须具有可选类型,而不是'String'”in“if let tempString = urlString {”

1 个答案:

答案 0 :(得分:0)

export function isAuthz(allowed, except, permissions) {
  // Now do permission check
  ...
}

你强行打开一个可选变量,你就会得到nil,所以应用程序崩溃了。

 let svc = SFSafariViewController(url: NSURL(string: urlString)! as URL)

这将有助于防止崩溃,你必须更换代码

本守则有效: -

if let tempString = urlString {
 let svc = SFSafariViewController(url: NSURL(string: tempString ) as URL)
}