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) Concurrency Writing Concurrent Networking Code

Update on the API regarding closures involving sharedSessions & relative URLs in Swift needs to be noted.

The following code pattern seemingly now needs to be noted regarding writing concurrent networking code in Swift as it relates to this course

override func viewDidLoad() {
    super.viewDidLoad()
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    println("Base URL", baseURL)
    let forecastURL = NSURL(string: "37.8267,-122.423/", relativeToURL: baseURL)

    //let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
    //println(weatherData)

    let sharedSession = NSURLSession.sharedSession()

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

    downloadTask.resume()

  }

Note that the forecastURL must now end with a "/" for the forecast.io API to correctly response to the URI for some strange reason, and the same variable must be unwrapped when the downloadTaskWithURL method is used with an instance of a shared session singleton.

Perhaps Pasan Premaratne can add the latter information in the Teacher Notes section.

4 Answers

Pasan Premaratne
STAFF
Pasan Premaratne
Treehouse Teacher

Hey Kevin Lozandier.

The issue with the unwrapping is because of the introduction of failable initializers in Swift. Some init methods now return an optional that needs to be unwrapped. Xcode should automatically resolve this for you, but I will add a note. Unfortunately it's a pretty vast topic to explain in just a note so I'm re-recording all this in a few days.

Regarding the forward slash for the URL, I haven't encountered that error. It works fine as it is when I run the app...so I couldn't tell you what's going on there honestly :/ Keep me posted if you find anything.

It's been 3 weeks, perhaps an update to the video could be made?

Thank you, Kevin, I would not have known why there was an error without this post.

Hey, Sean Conrad: Glad I could help! Pasan Premaratne will likely post an update here once he's finished with re-recording seemingly.