应用程序保持崩溃发现Nil Swift

时间:2023-01-22 15:47:05

I am trying to display a webview in my app. The url for the webview is in object format because i got it from json. So in my code i converted the object to NSString and tried to load the view but my app crashes saying this: fatal error: unexpectedly found nil while unwrapping an Optional value. Is there a better way to convert object to string? How can i get the webview to display the url content?

我想在我的应用程序中显示webview。 webview的url是对象格式,因为我是从json获取的。因此,在我的代码中,我将对象转换为NSString并尝试加载视图但我的应用程序崩溃说:致命错误:在展开Optional值时意外发现nil。有没有更好的方法将对象转换为字符串?如何让webview显示网址内容?

Where Detail Item is made:

制作详细项目的地方:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow() {
            let object: AnyObject = objectsPath[indexPath.row]
        (segue.destinationViewController as DetailViewController).detailItem = object
        }
    }
}

Here is my code:

这是我的代码:

  var detailItem: AnyObject? {
        didSet {
            // Update the view.
            self.configureView()
        }
    }

    func configureView() -> NSURLRequest {
        // Update the user interface for the detail item.
        let detail: AnyObject = self.detailItem!
        let stringURL: String = String(detail as NSString)
        let url = NSURL(string: stringURL)
        let request = NSURLRequest(URL: url!) <---ERROR occurs here
        return request



           // let string:St= detailItem!
          //  previewWebView.
           // println(string)
            //let url = NSURL(string: string)
            //let request = NSURLRequest(URL: url!)
            //self.previewWebView.loadRequest(request)

        }



    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.previewWebView.loadRequest(configureView())

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1 个解决方案

#1


0  

You are calling configureView() within your viewDidLoad() with no regard for the value of self. detailItem. You should be checking for a value before calling this function in viewDidLoad() or you should modify configureView() to not claim there is definitely a value of self.detailItem.

您在viewDidLoad()中调用configureView()而不考虑self的值。 detailItem。您应该在viewDidLoad()中调用此函数之前检查值,或者您应该修改configureView()以不声明self.detailItem的值。

#1


0  

You are calling configureView() within your viewDidLoad() with no regard for the value of self. detailItem. You should be checking for a value before calling this function in viewDidLoad() or you should modify configureView() to not claim there is definitely a value of self.detailItem.

您在viewDidLoad()中调用configureView()而不考虑self的值。 detailItem。您应该在viewDidLoad()中调用此函数之前检查值,或者您应该修改configureView()以不声明self.detailItem的值。