-1

I've been having this error for quite a while now and I have no idea how to fix it. "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" That's the exact error code that pops up. I know this might be a simple fix but I can't figure out how to fix this at all.

Here is my code for the webView Kit.

import UIKit
import WebKit


class ViewController: UIViewController {
    //webpage import

@IBOutlet weak var ParksView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: "https://www.google.com")
    let request = URLRequest(url: url!)

    ParksView.load(request)

    // Do any additional setup after loading the view, typically from a nib.
}

If someone could explain to me what I need to do to fix this problem, I would be forever grateful. Thank you!

  • 1
    There is literally like **millions** of questions about *Unexpectedly found nil while unwrapping an Optional value*. Please do better research next time! – LinusGeffarth Apr 19 '18 at 16:56

1 Answers1

0

It is failing on this line:

let request = URLRequest(url: url!)
ParksView.load(request)

Change it to this:

if let unwrappedURL = url {
    let request = URLRequest(url: unwrappedURL)
    ParksView.load(request)
}

Or:

guard url != nil else {return}

let request = URLRequest(url: url!)
ParksView.load(request)

This will make sure the url does not equal nil.

George
  • 25,988
  • 10
  • 79
  • 133