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
Kevin Gonzales
4,638 PointsCant get passed this question.
Your job is to create a data task (not a download task), named dataTask, with a completion handler that returns the results of the data task. Add this data task between the commented section. Once that's done, within the body of the data task, return the results to the fetchTreehouseBlogPosts method's completion handler.
my code
import Foundation
// Add your code below typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void)
// Add your code below func fetchTreehouseBlogPosts (completion: BlogPostCompletion) { let blogURL = NSURL(string: "http://blog.teamtreehouse.com/api/") let requestURL = NSURL(string: "get_recent_summary/?count=20", relativeToURL: blogURL)
let request = NSURLRequest(URL: requestURL!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration() let session = NSURLSession(configuration: config)
// Add your code between the comments let dataTask = session.dataTaskWithRequest(request){ (let data, let response, let error) in // Steps. 1 Check HTTP respnose for succesful get request. You can add diferent logic for diferent status codes, but we only did one- the succesful one. if let httpRespnose = response as? NSHTTPURLResponse{
switch (httpRespnose.statusCode){
case 200:
// 2. create JSON object with data
let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil , error: nil) as? [String:AnyObject]
completion(jsonDictionary)
default:
println("GET Request not succesful. The HTTP Status code is \(httpRespnose.statusCode)")
}
} else {println("error not a valid HTTP response ")}
}
// Add code above
dataTask.resume() }
The error I get:
swift_lint.swift:28:32: error: missing argument for parameter #2 in call completion(jsonDictionary) ^
Help please
3 Answers
Bruna Aleixo
2,693 Pointsimport Foundation
// Add your code below
typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void)
func fetchTreehouseBlogPosts(completion: BlogPostCompletion) {
let blogURL = NSURL(string: "http://blog.teamtreehouse.com/api/")
let requestURL = NSURL(string: "get_recent_summary/?count=20", relativeToURL: blogURL)
let request = NSURLRequest(URL: requestURL!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let dataTask: NSURLSessionDataTask = session.dataTaskWithURL(request) {
(let data, let response, let error) in
completion(data, response, error)
}
dataTask.resume()
}
Why isnt my code working??
Unsubscribed User
Courses Plus Student 22,129 PointsThe reason why your code is not working is because your dataTask is calling a session with a "dataTaskWithURL". to fix this problem simply change:
session.dataTaskWithURL(request)
// To
session.dataTaskWithRequest(request)
Your Final code should look as follows:
import Foundation
// Add your code below
typealias BlogPostCompletion = ((NSData!, NSURLResponse!, NSError!) -> Void)
func fetchTreehouseBlogPosts(completion: BlogPostCompletion) {
let blogURL = NSURL(string: "http://blog.teamtreehouse.com/api/")
let requestURL = NSURL(string: "get_recent_summary/?count=20", relativeToURL: blogURL)
let request = NSURLRequest(URL: requestURL!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let dataTask: NSURLSessionDataTask = session.dataTaskWithRequest(request) {
(let data, let response, let error) in
completion(data, response, error)
}
dataTask.resume()
}
Kevin Gonzales
4,638 PointsI think I did it wrong. us in my completion has 3 parameters ?
thomascawthorn
22,986 PointsI'm only just starting on completions right now! I'll let you know if I can help out afterwards ;)
Kevin Gonzales
4,638 PointsThanks!
Mobify CST
Courses Plus Student 3,841 PointsMobify CST
Courses Plus Student 3,841 PointsLook, your BlogPostCompletion accepts 3 parameters: data, response and error. Conveniently this is exactly what you have in the closure. So you can literally do what your task asks you to do: add the data task and return the results to the completion handler: