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 Photo Browser iPhone App Downloading Data with NSURLSession NSURLSession Basics

What's going wrong here? There is a mistake somewhere in this code, but I can't find it.

The Treehouse compiler won't open and let me see the error. This code seems to be working in X code, but it doesn't pass the coding challenge.

/* Add task 1 code here */
NSURL *url = [[NSURL alloc] initWithString: @"http://teamtreehouse.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];

/* Add task 2 code below */
NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        NSLog(@"Response:%@", response);
    }];

    [task resume];

1 Answer

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Try this:

/* Add task 1 code here */
NSURL *url = [[NSURL alloc] initWithString : @"http://teamtreehouse.com"];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];

/* Add task 2 code below */


NSURLSessionDownloadTask *task;

task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location,NSURLResponse *response, NSError *error){
NSLog(@"Response: %@", response);
}];

[task resume];

Thanks so much!!! :)

Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Thanks Cindy,

This one tripped me up a bit too, as I was doing it all with one line of code. It's weird the code checker won't take

NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location,NSURLResponse *response, NSError *error){
NSLog(@"Response: %@", response);
}];

[task resume];

as being correct. It's the same code, just on one line. ?? Gabe Nadel

:dizzy: