Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Weather App with Swift (Retired) Displaying Our Weather Data Connecting the UI to Our Model

Rami Ammoun
Rami Ammoun
7,468 Points

Need help with an Error here!

I'm getting an error when building the app after I wrote the dispatch_async lines. The app stops building pointing to an error at the bracket under the downloadTask.resume() line. In the debug area, if I press the "Continue program execution" the app runs normally and the error disappears.

My code is as follows:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var iconView: UIImageView!
@IBOutlet weak var currentTimeLabel: UILabel!
@IBOutlet weak var humidityLabel: UILabel!
@IBOutlet weak var precipitationLabel: UILabel!
@IBOutlet weak var summaryLabel: UILabel!
@IBOutlet weak var temperatureLabel: UILabel!




private let apiKey = "4de5319c1c8991a1ff2985013795b0b3"

override func viewDidLoad() {
    super.viewDidLoad()

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)


    let sharedSession = NSURLSession.sharedSession()
    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

        if (error == nil) {
            let dataObject = NSData(contentsOfURL: location)
            let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary


            let currentWeather = Current(weatherDictionary: weatherDictionary)




            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.temperatureLabel.text = "\(currentWeather.temperature)"
                self.iconView.image = currentWeather.icon!
                self.currentTimeLabel.text = "At \(currentWeather.currentTime!) it is"
                self.humidityLabel.text = "\(currentWeather.humidity)"
                self.precipitationLabel.text = "\(currentWeather.precipProbability)"
                self.summaryLabel.text = "\(currentWeather.summary)"

            })
            }
    })

    downloadTask.resume()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}

}

2 Answers

Rami Ammoun
Rami Ammoun
7,468 Points

I found the problem, it wasn't an error with the code, the "toggle global breakpoint state" button was "ON" and was holding the code from executing from the start till the end. Thank you Nejc

Hey welcome anyways :) Sorry I wasn't helpful.

Erase the ")" on the line before.