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

Issue with the update to Xcode 6.1 for Stormy

I'm having issues with the Stormy weather app course for the update to Xcode 6.1, where NSURL objects are now optionals. I'm kinda new to iOS and don't really understand the whole optional concept, and why it's better than just checking if the response is nil in an if statement. Anyways, I think I'm doing this right, but when I try to build I get the error Extraneous argument label 'contentsOfURL:' in call on the line 17 of the code below:

import UIKit

class ViewController: UIViewController {

    private let apiKey = "redacted but I do have the api key here"

    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: "37.8267,-122.423",
            relativeToURL: baseURL
        )
        let weatherData = NSData.dataWithContentsOfURL(
            contentsOfURL: forecastURL!,
            options: nil,
            error: nil
        )
    }

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


}

2 Answers

Chris Shaw
Chris Shaw
26,650 Points

Hi programmedpixel,

The problem is the following.

NSData.dataWithContentsOfURL

As of Xcode 6.1 there are no longer class function constructors, instead you just need to call the constructor from within an NSData instance and it will work the same way.

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

initWithContentsOfURL

Hi!

I tried exactly this, but I always get a nil. Any ideas why?

Damn, must not have read the teacher's notes correctly. Thanks.