Alex Petev Alex Petev - 2 months ago 18
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 = "http://google.com"

override func viewDidLoad() {
super.viewDidLoad()
loadAddressURL()
}

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

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


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

Answer

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!
Comments