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 Managing Complexity Methods with Closures

Trick Spades
Trick Spades
3,182 Points

Cannot invoke dataTaskWithRequest - XCode 7

Updated to XCode 7, and now it seems that my code is incorrect.

Receiving error "Cannot invoke 'dataTaskWithRequest' with an argument list of type" There is another post by someone who claims he fixed it, but when I use it, data and response become unresolved identifiers.

Here is my downloadJSONFromURL Method

    func downloadJSONFromURL(completion: JSONDictionaryCompletion) {

        let request: NSURLRequest = NSURLRequest(URL: queryURL)
        let dataTask = session.dataTaskWithRequest(request) {
            (let data, let response, let error) in

            // 1. Check HTTP response for successful GET request

            if let httpResponse = response as? NSHTTPURLResponse {
                switch(httpResponse.statusCode) {
                case 200:
                    // 2. Create JSON object with data
                    let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject]
                    completion(jsonDictionary)
                default:
                    print("GET request not successful HTTP status code: \(httpResponse.statusCode)")
                }

            } else {
                print("Error: Not a valid HTTP response")
            }



        }
        dataTask.resume()

    }
}

4 Answers

Jordan Morano
Jordan Morano
8,737 Points

I was having the same issue. Luckily someone posted an answer on StackOverflow:

http://stackoverflow.com/questions/31296545/correct-handling-of-nsjsonserialization-try-catch-in-swift-2-0

You essentially want to do this =>

do {
  let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! [String: AnyObject]
  completion(jsonDictionary)
} catch {
  print(error)
}
Trick Spades
Trick Spades
3,182 Points

Thanks a lot man. No errors for now, but hopefully it won't mess anything up for the rest of the course.

Trick Spades
Trick Spades
3,182 Points

Aaron Ackerman, hey man if you're not busy. I could use your help.

Tukang Keboen
Tukang Keboen
2,840 Points

Somebody help.. same stuck even i try to put " do { " seem also cannot invoke 'dataTaskWithRequest'

2 days already i have been stuck there.. help to solve asap.. thank' you

Kevin Kirsche
Kevin Kirsche
9,588 Points

This worked for me. Thank you!