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 trialTrick Spades
3,182 PointsCannot 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
8,737 PointsI was having the same issue. Luckily someone posted an answer on StackOverflow:
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
3,182 PointsAaron Ackerman, hey man if you're not busy. I could use your help.
Tukang Keboen
2,840 PointsSomebody 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
9,588 PointsThis worked for me. Thank you!
Trick Spades
3,182 PointsTrick Spades
3,182 PointsThanks a lot man. No errors for now, but hopefully it won't mess anything up for the rest of the course.