Alex Petev Alex Petev - 1 year ago 96
Swift Question

EXC_BAD_INSTRUCTION in Swift when loading a WebView

I followed a tutorial to make a web view on an iOS app and I don't get any errors. However, when I build the app to test it on the Simulator, I get the error:

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Here is my ViewController.swift

import UIKit

class ViewController: UIViewController {
@IBOutlet var Webview : UIWebView!
var URLpath = ""

override func viewDidLoad() {

override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func loadAddressURL(){
let requestURL = NSURL(string:URLpath)
let request = NSURLRequest(URL:requestURL!)

When I run the app the
is marked in red and displays the error.
In the debugger I get "fatal error: unexpectedly found nil while unwrapping an Optional value"

Answer Source

You are for sure made mistake in connection your UIWebView from storyboard/xib to code. You should remove existed connection in storyboard/xib, delete line

@IBOutlet var Webview : UIWebView!

and then connect it again. Proper declaration should contain word weak in order:

@IBOutlet weak var Webview : UIWebView!