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("°", 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