0

I have been struggling with this for a while. I am trying to have a UIWebview and I keep getting a fatal error which is as follows "fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)". Then in my code it says "Thread 1: EXC_BAD_INTRUCTION(code=EXC_1386_INVOP,subcode 0x0". I do not understand why I keep getting this. Also, I am very new to coding. I will post the code below.

class ViewController: UIViewController {
    @IBOutlet var WebView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let URL = NSURL(string: "http://www.google.com")

        WebView.loadRequest(NSURLRequest(URL: URL!))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Laura Calinoiu
  • 704
  • 1
  • 8
  • 24
  • FYI: it is crashing on WebView.loadRequest(NSURLRequest(URL: URL!)) – Connor Stocker Jan 17 '16 at 17:25
  • In order to code in Swift you *have* to understand how Optionals work. Read the Swift manual. // Also see http://stackoverflow.com/questions/24018327/what-does-an-exclamation-mark-mean-in-the-swift-language. Either your IBOutlet or your URL is nil. – Eric Aya Jan 17 '16 at 17:25
  • try `print(WebView)` right before the crashing line - what does it yield? – luk2302 Jan 17 '16 at 17:26

2 Answers2

0

Most probable cause is that you forgot (or lost) the link to your IBOutlet

Alain T.
  • 40,517
  • 4
  • 31
  • 51
0

The above code works fine for me. Make sure that you properly connected the IBOutlet. My code

And also make sure that you added App Transport Security in your plist. Like below screenshot plist

Nayana
  • 128
  • 9