-3

Trying to create a webview controller in Swift 3. On the last line of the viewDidLoad() function, I am getting the error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

As far as I can tell, this error should only happen if something isn't set, but webView is set, loadRequest exists, and the request variable exists. Am I not wrapping something correctly? Or has the syntax changed somewhere?

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL (string: "https://example.com");
    let request = URLRequest(url: url!);
    webView.loadRequest(request);
}

I haven’t found anything that explained exactly what was going on well. I tried to document as best as I was able.

This does not appear to be related to the wrapping issue.

jscs
  • 63,095
  • 13
  • 148
  • 192
Steven Matthews
  • 9,141
  • 40
  • 114
  • 203
  • do not ever use force unwrapping in any case. – Dominik Bucher Mar 03 '18 at 14:35
  • 3
    Did you *verify* that `webView` is not nil? – Martin R Mar 03 '18 at 14:37
  • webView is just a UIWebView I think? – Steven Matthews Mar 03 '18 at 14:39
  • On which line does the app crash? on line with webView or on line with request assignment? – Dominik Bucher Mar 03 '18 at 14:40
  • It was on the line with the webView. – Steven Matthews Mar 03 '18 at 14:41
  • verify that the webview is connect to interface builder – Sh_Khan Mar 03 '18 at 14:41
  • @Sh_Khan - I'll research how to do that. – Steven Matthews Mar 03 '18 at 14:43
  • Please stop trying to "address" downvoters in your question. They will comment if they have something useful to say. https://meta.stackoverflow.com/questions/342288/mention-downvoters – jscs Mar 03 '18 at 14:44
  • @JoshCaswell - this example isn't directly relevant to my post. I'm not asking downvoters for feedback. I'm pointing out that my experience level in this technology is fairly low and to please be mindful of that. That question that you linked to is about being able to contact downvoters via messages or for clarification. They're not the same. – Steven Matthews Mar 03 '18 at 14:49
  • The point is that trying to contact specific people, especially downvoters, via edits to your question is not what questions are for. – jscs Mar 03 '18 at 14:50
  • 1
    @DominikBucher: *"do not ever use force unwrapping in any case"* – I disagree. If an optional being nil indicates a *programming error* (not a runtime condition) then force-unwrapping can be appropriate. There are also other cases where one *knows* that the result cannot be nil. – Martin R Mar 03 '18 at 14:57

2 Answers2

2

You should unwrap safely, then step trough your code wit the debugger to identify the variable set to nil.

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    guard let url = URL(string: "https://example.com") else {
        return
    }
    let request = URLRequest(url: url)
    webView?.loadRequest(request)
}
sundance
  • 2,754
  • 1
  • 18
  • 25
  • this won't solve the problem only prevent the crash – Sh_Khan Mar 03 '18 at 14:52
  • 1
    For a *fixed* URL, force-unwrapping can be appropriate in order to detect programming errors early. Also `webView?.loadRequest` would just (silently) *hide* a programming error (missing connection) – Martin R Mar 03 '18 at 14:54
1

UIWebView is deprecated, so you should probably be using WKWebView at some point.

First thing's first:

1) No semicolons are necessary in Swift unless you're putting multiple commands on the same line.

2) Check your connections to your IBOutlet. I suspect your problem lives here. In Xcode, here's where you'd take a peek at them. The easiest thing to do would be to disconnnect and reconnect them.

enter image description here

3) Stay away from force-unwrapping Optionals. Instead, make use of a guard statement, like so:

guard let url = URL(string: "https://www.youtube.com/watch?v=dQw4w9WgXcQ") else { return }
// now the optional URL is unwrapped without risking a crash.
let request = URLRequest(url: url) 
webView.loadRequest(request)

Here's a link to a repo I threw up.

Adrian
  • 15,330
  • 17
  • 106
  • 173
  • I have to use UIWebView - I get an WkWebView before iOS 11 error otherwise. I am not skilled enough to debug whatever upgrading might require and the last team to work on this apparently used iOS 10, so I'll go with that. I do think that it has something to do with the IB connection. I'll take a look at your repo. – Steven Matthews Mar 03 '18 at 15:30
  • All I really want is for a scene to go to a specific URL. – Steven Matthews Mar 03 '18 at 15:31