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) Pulling Data From the Web Making a Network Call

Gil Aguilar
Gil Aguilar
8,813 Points

I am having a problem making the first network call and need help figuring it out ...

Here is the code snippet. I also pushed the Stormy project to github.

import UIKit

class ViewController: UIViewController {

private let apiKey = "175b7c75e88f13aaf638a31fe53ce93b"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")

    let forecastURL = NSURL(string: "43.650165, -116.279280", relativeToURL: baseURL)

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

    println(weatherData)
}

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

}


github page for the code. I was following along and then I came across this error :

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


https://github.com/TGrooves-208/Stormy

Any help would be greatly appreciated thanks guys :-)

2 Answers

Hey so i copied your code and tried it. At first it didn't work until I took out the spaces in your strings:

let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")

let forecastURL = NSURL(string:"43.650165,-116.279280", relativeToURL: baseURL)

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

println(weatherData) 

This should work.

I took out the spaces in your strings for forecastURL and baseURL.

Gil Aguilar
Gil Aguilar
8,813 Points

Thanks for that Ronald I am severely dyslexic and it's things like that that trip me up for a tad now I can go ahead and knock this out. Amazing thing that I always forget sometimes space does matter cheers and thanks again. Worked as it should have ;-)

No problem i'm happy it worked :)