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

Compiler Error: Value of optional type 'NSURL?' not unwrapped; did you mean to use '!' or '?'?

import UIKit

class ViewController: UIViewController {


    private let apiKey = "a763da8a149b906e8ba708025a00fd13"


    override func viewDidLoad() {
        super.viewDidLoad()

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forcastURL = NSURL(string: "45.488746,-122.749306", relativeToURL: baseURL)

        let weatherData = NSData.dataWithContentsOfURL(forcastURL, options: nil, error: nil)
        println(weatherData)

    }

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


}

Any help would be appreciated.

2 Answers

Mike Rogers
Mike Rogers
5,280 Points
    override func viewDidLoad() {
        super.viewDidLoad()

        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")!
        var forcastURL = NSURL(string: "45.488746,-122.749306", relativeToURL: baseURL)!

        var weatherData = NSData(contentsOfURL: forcastURL)
        println(weatherData)

    }

I added in the "!" after setting the NSURL, then swapped out the dataWithContentsOfURL (Which looks to not be in XCode any more) with NSData(contentsOfURL: and it worked.

This was a HUGE help! I was wondering why xcode wasn't autocompleting the code string for me lol.

Thanks!

This works. Thanks!