Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 :)