-3

Here is my code:

 override func viewDidAppear(animated: Bool) {

    if let vid = self.selectedVideo {

        self.titleLabel.text = vid.videoTitle
        self.descriptionLabel.text = vid.videoDescription

        let width = self.view.frame.size.width
        let height = width/320 * 180

        let videoEmbedString = "<html><head><style type=\"text/css\">body {background-color: transparent;color: white;}</style></head><body style=\"margin:0\"><iframe frameBorder=\"0\" height=\"" + String(height) + "\" width=\"" + String(width) + "\" src=\"http://www.youtube.com/embed/" + vid.videoId + "?showinfo=0&modestbranding=1&frameborder=0&rel=0\"></iframe></body></html>"

        self.webView.loadHTMLString(videoEmbedString, baseURL: nil)
    }
}

I have problems on the last line:

fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

Rob
  • 415,655
  • 72
  • 787
  • 1,044

2 Answers2

3

Confirm that the outlet is hooked up correctly by examining the left margin and making sure you have a solid dot next to each @IBOutlet. For example, below, since there is an empty dot next to the webView declaration, I know I didn't hook up the outlet correctly, and therefore it will be nil resulting in an error like yours:

enter image description here

Whenever you get this error, "unexpectedly found nil", identify which variable was nil and then diagnose why it might be so.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
1

Your issue is that webView is probably declared as a (weak) implicitly unwrapped optional type and that is causing the error message to appear when you are trying to use it. Wrap your code with a guard and make sure you have connected your webview correctly in IB.

John Difool
  • 5,572
  • 5
  • 45
  • 80