-2

Im using XCODE and Swift 3.

I have an app which allows the user to change what site they are viewing, and which button they press then updates the webview with the new url.

I seem to constantly get this error : "unexpectedly found nil while unwrapping an Optional value"

I am new to Xcode and Swift, this is my first app build. So trying to debug it is proving difficult. I can see that the URL i am using does have a "?" after it. Is there a way I can make the url a non optional value? And when I do, would the webview update accordingly with the new url

enter image description here

Levi S
  • 45
  • 1
  • 10
  • Can you please provide an example of your code where it is crashing – David Williames Apr 19 '17 at 13:35
  • show the code where you are facing problem – Lalit kumar Apr 19 '17 at 13:35
  • Sure, I have a screenshot I will add in now. – Levi S Apr 19 '17 at 13:40
  • Please learn how to use Optionals in Swift, this is very important. Read the duplicate link, but most importantly read the [Swift manual, the Optionals chapter](https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjy0LH84LDTAhUBrRoKHfYbA-IQygQIKDAA&url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fcontent%2Fdocumentation%2FSwift%2FConceptual%2FSwift_Programming_Language%2FTheBasics.html%23%2F%2Fapple_ref%2Fdoc%2Fuid%2FTP40014097-CH5-ID330&usg=AFQjCNHOuBMHtgkI642rFokkBTRMnNEFDg). – Eric Aya Apr 19 '17 at 14:44

4 Answers4

0

Make sure that in your storyboard, your button is linked to an IBOutlet if you have one in the code.

Antoine
  • 1,139
  • 9
  • 21
0

Use this

self.webView?.loadRequest(requestobj)
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

Just for debugging, try:

let bleh = URL(string: "www.wikipedia.org")

Pretty sure your address has an issue where bleh is nil.

unixb0y
  • 979
  • 1
  • 10
  • 39
0

In swift you can define a variable as an "implicitly unwrapped optional":

var webView: UIWebView!

That tells the compiler that any time you try to reference the variable, just unwrap it, as if you put and exclamation point after.

By default, outlets are set up as implicitly unwrapped optionals. If they're nil, you crash.

My guess is that your webView is an outlet, and it's nil. It could also be that your requestObj variable is an implicitly unwrapped optional. Check to see if either of those is nil.

If you still can figure it out, post the code that declares both of those variables.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • This is the code in viewcontroller.swift: `code func loadAddressUrl(){ let bleh = URL(string: "https://google.com") print (bleh) // Outputs Optional(https://google.com) let requestObj = URLRequest(url: bleh!) print (requestObj) // Outputs https://google.com webView.load(requestObj) }` – Levi S Apr 19 '17 at 14:01
  • This is the code in menu-controller.swift: `code @IBAction func buttonchangeURL(_ sender: Any, forEvent event: UIEvent) { let cake = ViewController() cake.loadAddressUrl() } ` when the button is pressed, it triggers the viewcontroller function loadAddressUrl – Levi S Apr 19 '17 at 14:04
  • I did a print of webview, and it logs "NIL" - so do i just need to correct add the url to the webview.load ? – Levi S Apr 19 '17 at 14:10
  • Don't post code in comments. It's unreadable. Edit your question to add new code at the end. – Duncan C Apr 19 '17 at 14:46
  • As for your follow-on question, yes. You need to fix the broken outlet, which is a very common cause of problems. "When something goes wrong, check your outlet and IBAction connections". – Duncan C Apr 19 '17 at 14:47