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 trialJeremy Taitelbaum
4,680 PointsI am unclear as to what is being asked in the first part of the code challenge.
I guess I just don't fully understand what I am doing wrong in part one of this code challenge. I have tried this using the current syntax of Xcode 7 as well as the old syntax used in the previous video. I have tried using the func fetchTreehouseBlogPosts independent of a class called NetworkOperations, in which I assign an arbitrary URL to the queryURL, and change session and config to simple variables (eliminating the self. prefix from config in the assignment of the session variable). I have configured this to assume we are fetching a JSON file cast as a dictionary, and I have configured it to simple fetch raw NSData. In this case the completion handler simply takes NSData as a parameter. No matter what I try, I get the same "Bummer!: Make sure the method is called fetchTreehouseBlogPosts and that you are using correct syntax!" error, even though the preview button returns no errors (when I use the old syntax portrayed in the video). Can someone please explain what I am doing wrong?
import Foundation
// Add your code below
class NetworkOperations {
lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL
typealias BlogPostCompletion = ([String: AnyObject]?) -> Void
init(url: NSURL){
self.queryURL = url
}
func fetchTreehouseBlogPosts (completion: BlogPostCompletion) {
let request: NSURLRequest = NSURLRequest(URL: queryURL)
let dataTask = session.dataTaskWithRequest(request) {
(let data, let response, let error) in
if let httpResponse = response as? NSHTTPURLResponse {
switch(httpResponse.statusCode) {
case 200:
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 respone")
}
}
dataTask.resume()
}
}
Jeremy Taitelbaum
4,680 PointsJeremy Taitelbaum
4,680 PointsNever mind. I figured it out. I was overthinking it.