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 Writing Concurrent Networking Code

David Bauer
David Bauer
3,863 Points

Cannot invoke "dataTaskWithRequest" with an argument list of type "(NSURLRequest, completionHandler: (NSData!,...

Hi, I added my code like Pasan did and I'm getting multiple errors. Here is my code:

        // Use NSURLSession API to fetch data
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        let session = NSURLSession(configuration: configuration)

        // NSURLRequest Object
        let request = NSURLRequest(URL: forecastURL!)
        let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in
            print(data)
        })

I am using Xcode 7 beta with Swift 2.0 Does anyone know what I did wrong here?

Thanks!

Piplup Pochama
Piplup Pochama
16,851 Points

Thanks for this! Been trying to find the answer for this.

Any chance you know how to fix the NSData version from the earlier lecture?

        let weatherData = NSData(contentsOfURL: forecastURL!, options: nil, error: nil)
David Bauer
David Bauer
3,863 Points

Piplup Pochama I haven't the code anymore but I'm sure I solved this problem. Can you paste more code in here so that I can look at it?

Piplup Pochama If i can remember correctly in xCode 7 swift 2.1 you can remove the option and error from the argument like so:

  let weatherData = NSData(contentsOfURL: forecastURL!)

try that and let me know if you have any success

2 Answers

David Bauer
David Bauer
3,863 Points

Ok, seems like I found the answer:

       let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
            print(data)
        })

Thanks David for the answer.

@ Piplup Pochama the error in

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

got fixed when I Used the below code instead

let weatherData = NSData(contentsOfURL: forecastURL!)