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

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!