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 trouble

Hello,

I'm having trouble with the Blog Reader App tutorial videos.

I've been following the videos no problem using the videos url example but the iOS simulator just displays empty cells when I use a url of my own.

Can anyone help? This is what I have...

NSURL *blogURL = [NSURL URLWithString:@"http://theforeground.co.uk/"];

    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:@"title"];

    for (NSDictionary *bpdictionary in blogPostsArray) {
        BlogPost * blogPost = [BlogPost blogPostWithTitle:[bpdictionary
            objectForKey:@"title"]];
        blogPost.author = [bpdictionary objectForKey:@"author"];
        blogPost.thumbnail = [bpdictionary objectForKey:@"thumbnail"];
        [self.blogPosts addObject:blogPost];
    }

4 Answers

Dimitris Sotiris Tsolis
Dimitris Sotiris Tsolis
28,141 Points

Good work. Now, about the error, it fails because you have to change your code to fit theforeground's feed and not the teamtreeehouse's feed. For example, using NSJSONSerialization, you create an NSDictionary but in fact, you have an NSArray. Each item in this array is a dictionary. So, instead of this

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.blogPosts = [NSMutableArray array];
NSArray *blogPostsArray = [dataDictionary objectForKey:@"title"];

you can try the following

self.blogPosts = [NSMutableArray array];
NSArray *blogPostsArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
Dimitris Sotiris Tsolis
Dimitris Sotiris Tsolis
28,141 Points

The url you use, is the actual website and not a url which returns a JSON formatted response. I noticed that it's a wordpress blog so we have 2 options.

Option 1: Use "http://theforeground.co.uk/feed/" for url and parse the xml file (instead of json)

Option 2: Use a plugin like this one http://wordpress.org/plugins/feed-json/ to convert the feed into json and then change the url to the corresponding format (if you use this plugin, to "http://theforeground.co.uk/feed/json

thank you for your quick reply!

i had installed the Feed JSON plugin, and changed the url, however when i run the app it breaks in the Main.

It fails here:

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

with the message 'unrecognised selector'

That's amazing! thank you for helping out this novice.

Now I have a good point to carry on from. Thank you