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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Brad Forsyth](https://secure.gravatar.com/avatar/b12d4f83c7f7999ad99953e68807db63?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Brad Forsyth
7,623 PointsInitialization of immutable value 'dataTask'
I'm not getting any errors, but I am getting the following issue (the yellow triangle in the gutter): "Initialization of immutable value 'dataTask' was never used; consider replacing with assignment to '_' or removing it"
Here is my code:
class NetworkOperation {
lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL
typealias JSONDictionaryCompletion = ([String: AnyObject]?) -> Void
init(url: NSURL) {
self.queryURL = url
}
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
do {
let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
completion(jsonDictionary)
} catch let error {
print("JSON Serialization failed. Error: \(error)")
}
default:
print("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
}
} else {
print("Error: Not a valid HTTP response")
}
}
}
}
So what's my problem?
2 Answers
![Michael Hulet](https://uploads.teamtreehouse.com/production/profile-photos/672712/micro_me.jpg)
Michael Hulet
47,912 PointsThe problem is that you created the dataTask
constant, but you never use it at all. To fix the warning (and also actually download data), you need to tell the task to start, using the resume
method, like this:
let dataTask = session.dataTaskWithRequest(request) { (let data, let response, let error) in
// All your handling code here omitted for brevity
}
// Right after you create the task, you need to start it
dataTask.resume()
![Atif Naqvi](https://secure.gravatar.com/avatar/b04fafb9a3a397d9c412598f6005f6bd?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Atif Naqvi
4,144 PointsGlad you posted this!
Brad Forsyth
7,623 PointsBrad Forsyth
7,623 PointsWow, I can't believe it was that simple. Thanks!