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 Interacting with Data From the Web Fetching Data

Mateo Sossah
Mateo Sossah
6,598 Points

Error with contentsofURL

When I try to create the weatherData constent, I do not find the NSData(contentsOfURL: , options: , error: ) structure and get the following error when I use it: "Arguments lables do not match any available overloads". Any idea why? Thanks a lot

Here is my code:

override func viewDidLoad() { super.viewDidLoad()

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(forecastAPIKey)/")
    let forecastURL = NSURL(string: "37.8262, -122.423", relativeToURL: baseURL)
    let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
Shandas Duchintav
Shandas Duchintav
8,222 Points

Hi! I got the same error as you and I think it's just that XCode version in the video is older and we are using the updated version. However, I could be wrong.

This is how I fixed the error.

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

The error won't go away, however my code was executed and I had 1 less call count on DarkSky.

Cheers

3 Answers

Inkwon Kim
PLUS
Inkwon Kim
Courses Plus Student 11,889 Points

Change : let weatherData = try? NSData(contentsOfURL: forecastURL!, options: [])

Atif Naqvi
Atif Naqvi
4,144 Points

Thanks. This took care of the warning, currently using xcode 7 and swift 2.0. I hope it works for the rest of the tutorial.

print(weatherData) did not work with this fix.

I do not understand whats going on with try?, and the options: [], chalking it up to swift 2 changes

Eric Aichele
Eric Aichele
4,710 Points

It's been a while since Swift 2.0 has released. Can we get an update to any of this? A Google Doc or SOMETHING?

Here's what worked for me:

// Data Object to fetch weather data

    let weatherData: NSData

    do {
    weatherData = try! NSData(contentsOfURL: forecastURL!, options: [])
    } catch  {
       print("error")
    }

    print(weatherData)