fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)

I have no idea what to do. I am building a weather app. I created a variable with the url and I know the url works but I need the data for the place the user types into the text field. So I concatenate my UITextField in the correct place in the url and I get this error in the log.


fatal error: unexpectedly found nil while unwrapping an Optional value

(lldb)


here is my code:





import UIKit

class ViewController: UIViewController {



@IBOutlet weak var textfield1: UITextField!


@IBOutlet weak var outputlbl: UILabel!


@IBAction func findtheweather(sender: UIButton) {

let url = NSURL(string: "http://www.weather-forecast.com/locations/" + textfield1.text! + "/forecasts/latest")!


let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) -> Void in

if let urlcontent = data {

let webcontent = NSString(data: urlcontent, encoding: NSUTF8StringEncoding)

let websitearray = webcontent!.componentsSeparatedByString("3 Day Weather Forecast Summary:</b><span class=\"read-more-small\"><span class=\"read-more-content\"> <span class=\"phrase\">")

if websitearray.count > 1 {

let weatherarray = websitearray[1].componentsSeparatedByString("</span>")

if weatherarray.count > 1 {

let weathersummary = weatherarray[0].stringByReplacingOccurrencesOfString("&deg;", withString: "°")

dispatch_async(dispatch_get_main_queue(), { () -> Void in

self.outputlbl.text = weathersummary

})

}

}

}

}

task.resume()

}





override func viewDidLoad() {

super.viewDidLoad()

/

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}






Help would be much appreciated,



Thanks

This error means that you’ve unwrapped an option (either explicitly, using

!
, or by accessing an implicitly unwrapped optional) and it’s nil. The code you posted has a bunch of optional unwraps, both explicit and implicit, so it’s hard to say what’s going wrong. What line does the debugger show when you see the failure?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
 
 
Q