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

Luke Telford
Luke Telford
2,931 Points

why don't i see the option for dataWithContentsOfURL ?

I'm following along with the tutorial and I'm having a problem, when Pasan is creating the weatherData constant when he's typing the code he gets multiple options after typing 'NSData.data' i only get one and its not the one i need. Is there any additional options i need to enable?

1 Answer

Chris Shaw
Chris Shaw
26,650 Points

Hi Luke,

I would advise you to have a read through the teacher notes as Xcode 6.1 introduced some breaking changes which are covered in the notes.

Luke Telford
Luke Telford
2,931 Points

ah thank you! i have done the changes that are provided but it still doesn't work, its saying 'fatal error: unexpectedly found nil while unwrapping an Optional value' in my console.

Chris Shaw
Chris Shaw
26,650 Points

luke telford, could you please post the code you currently have, thanks.

Luke Telford
Luke Telford
2,931 Points

yea sure,

import UIKit

class ViewController: UIViewController {

    private let apiKey = "8090fe1c97efed622a3b44f9f777ed7c"

    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: "53.819912, -1.477529", 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.
    }



}
Chris Shaw
Chris Shaw
26,650 Points

Hi luke telford,

The reason you're getting a nil error is because you have a space in your coordinates which has caught quite a number of users of guard as NSURL objects don't like spaces unless they're encoded which we don't need to do here so we can simply remove it.

let forecastURL = NSURL(string: "53.819912,-1.477529", relativeToURL: baseURL)
Luke Telford
Luke Telford
2,931 Points

Ah something so simple! it now runs but its returning nil in my command line where as Pasan's return a long Hex value, is this normal with the changes that have happened in Xcode or should i be also get a Hex value?

Chris Shaw
Chris Shaw
26,650 Points

I just tried your code and it worked fine, sometimes the simulator can have issues with HTTP requests, I would suggest you reset it via the Reset Content and Settings menu item under iOS Simulator as seen below.

Reset iOS Simular Content and Settings

Luke Telford
Luke Telford
2,931 Points

ah i see, i have done that and although I'm still getting nil as an output in my console the app itself runs with no error popping up

Luke Telford
Luke Telford
2,931 Points

I just downloaded the files from the course and its giving me the same error on mine so I'm guessing something has changed in Xcode? its saying "Value of optional type 'NSURL?' not unwrapped, did you mean to use '!' or '?'?

this is the line of code giving me this error

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