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

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,676 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.