Unable to use WKWebView, gives me an error

Hi this is my first time using these forums, how would I fix this issue:


My code is here:


import Cocoa
import WebKit

class ViewController: NSViewController, WKNavigationDelegate {
   
    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        webView.navigationDelegate = self
        let url=URL(string: "https://www.example.com")
        webView.load(URLRequest(url: url!))
       
        // Do any additional setup after loading the view.
    }
   
    override var representedObject: Any? {
        didSet {
            // Update the view, if already loaded.
        }
    }
}


The issue happens here:

webView.navigationDelegate = self


And gives the error:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value


What am I doing wrong?

Did you hookup your webView outlet to an instantiated WKWebView? Your code doesn't show that you created WkWebView instance, and the construction you are using looks like what you would nominally do if you were using Interface Builder to wireup views and IBOutlets. I suspect that your webView variable is nil because there is nothing hookeup to the IBOutlet.

Unable to use WKWebView, gives me an error
 
 
Q