Search code examples
iosswiftuiwebviewuisplitviewcontroller

Split View Controller and UIWebView


I am attempting to use a split view controller with a Detail View Controller that shows a web site. The code of the Detail View Controller is as follows:

class DetailViewController: UIViewController {

    @IBOutlet weak var webView: UIWebView!

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

    func configureView() {
        // Update the user interface for the detail item.
        if let detail = self.detailItem?.html {
            let url = NSURL(string: "http://myWebsite.com" + detail)
            println(url)
            let requestObj = NSURLRequest(URL: url!)
            println(requestObj)
            webView.loadRequest(requestObj)
            }
        }
    }

When I run the program, both println() show results. But the program throws an exception:

Optional(http://myWebsite.com/foo)

{ URL: http://myWebsite.com/foo } fatal error: unexpectedly found nil while unwrapping an Optional value

Why did it find nil? The optional value has a value?


Solution

  • Your webView is nil

    Your didSet listener is calling your code before you webView object exists. You need to ensure you webView exists by the time you call it.