"Saving Data on a Server with Swift 2" was retired on May 31, 2020.

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

Blog Reader App: NSDATA nil

Hi, I´m doing the Blog Reader App and I have a problem when I tried to access the URL data and run the app, that send me an error, hope some one could help me with this, thanks for your time. using: iOS7 / xcode 5

The CODE : '''

  • (void)viewDidLoad { [super viewDidLoad];

    NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL]; NSLog(@"%@",jsonData);

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@",dataDictionary);

    self.blogPosts = [dataDictionary objectForKey:@"posts"]; } ''' The Error : 1970-01-08 13:01:58.-351 BlogReader[901:60b] (null) 1970-01-08 13:01:58.-349 BlogReader[901:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' *** First throw call stack: (0x30cfde83 0x3b05a6c7 0x30cfddc5 0x31685be7 0x77c5b 0x3348695b 0x33486719 0x3348d3f1 0x3348aae5 0x334f582d 0x334f25fd 0x334ecb41 0x33487a07 0x33486cfd 0x334ec321 0x3596c76d 0x3596c357 0x30cc8777 0x30cc8713 0x30cc6edf 0x30c31471 0x30c31253 0x334eb5c3 0x334e6845 0x78139 0x3b553ab7) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

7 Answers

Thanks a lot man!, I got it!!, I do not what happened, but now it´s running the data, I appreciate so much your time and the help you gave to beginners like me :), have a great day.

i would double check that your treehouse url is correct and the one used in the video

Thanks !, I already did that and it´s the same URL, I´d tried that URL on a Browser and there is the data, some other ideas?, thanks for your help.

ok does the json data get logged with this statement? or is that what throws the exception?

 NSLog(@"%@",jsonData);

That throws me this :

** (null)**

does that console output match what is in the video?

Nop, that NSLog supposed to output the data in the URL, and I already downloaded the exercise file to verify and do the same thing, You think could be something with the XCODE 5 or iOS7?, because that video was made in a previous XCODE version and in iOS6 I think, thanks again

its possible but I doubt thats what it is. Can you paste the view did load method thats in the project file you downloaded so we can compare yours to the instructors?

'''- (void)viewDidLoad { [super viewDidLoad];

NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSLog(@"%@",jsonData);

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@",dataDictionary);


self.blogPosts = [dataDictionary objectForKey:@"posts"];

}'''

this is what I have in my view did load method of the table view controller and its working fine. Try this:

[super viewDidLoad];




    NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];

    NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];



    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.blogPosts = [NSMutableArray array];

    NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"];

    for (NSDictionary *bpDictionary in blogPostsArray) {

        BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];

        blogPost.author = [bpDictionary objectForKey:@"author"];

        blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];

        blogPost.date = [bpDictionary objectForKey:@"date"];

        blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];

        [self.blogPosts addObject:blogPost];
    }