1
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var cityNameTextField: UITextField!

@IBOutlet weak var cityNameLabel: UILabel!

@IBOutlet weak var cityTempLabel: UILabel!

@IBAction func getDataButtonClicked(sender: AnyObject) {

    getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=\(cityNameTextField.text)&APPID=6de03a1d1554874e7594a89fad719dd0")
}


override func viewDidLoad() {
    super.viewDidLoad()
    getWeatherData("http://api.openweathermap.org/data/2.5/weather?q=London&APPID=6de03a1d1554874e7594a89fad719dd0")
    // Do any additional setup after loading the view, typically from a nib.    
}

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

func getWeatherData(urlString: String) {
    let url = NSURL(string: urlString)

    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        dispatch_async(dispatch_get_main_queue(), {
            self.setLabels(data!)
        })   
    }
    task.resume()        
}


 var jsonData: AnyObject?

func setLabels(weatherData: NSData) {


    do {

        self.jsonData = try NSJSONSerialization.JSONObjectWithData(weatherData, options: []) as! NSDictionary

    } catch {
        //error handle here

    }

    if let name = jsonData!["name"] as? String {

        cityTempLabel.text = "\(name)"

    }



    if let main = jsonData!["main"] as? NSDictionary {
        if let temp = main["temp"] as? Double {
            cityTempLabel.text = String(format: "%.1f", temp)
        }
    }
}

};

Yesterday I had the app running and this morning I've just been getting new error messages that won't even allow the code to be compiled. They say 'Missing "Default-568h@2x.png" launch image' and 'Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftcode'. Thanks in advance.

bourvill
  • 152
  • 1
  • 13
Ben Udall
  • 11
  • 1
  • Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1 – Ben Udall Jul 29 '16 at 09:39
  • Try cleaning your project. Also, did you check this link - http://stackoverflow.com/questions/30848208/new-warnings-in-ios-9 – Yash Tamakuwala Jul 29 '16 at 18:40

1 Answers1

0

You need to add something to your info.plist file:

enter image description here

It's because the URL link you're trying to get data from is not a secured link, so adding this to you info.plist allows you to access that link. Just go to you info.plist and right-click and select Add Row, then add exactly what you see in the image above.

Also, remove the getWeatherData function from the viewDidLoad method, as you don't need that, as you call it when you press your button.

Also, I've noticed one of your labels isn't set correctly in your setLabels function, as they both try to set the cityTempLabel label, so update the other one to be cityNameLabel.

Build and run and it should all work.

Nick89
  • 2,948
  • 10
  • 31
  • 50