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

I cant understand why objective c is not retrieving the JSON data?I pasted my implementation(Treehouse Blog App)

What am I doing wrong here?

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

    NSData *jsondata=[NSData dataWithContentsOfURL:treehouse];

    NSError *error=nil;

    NSDictionary *json=[NSJSONSerialization JSONObjectWithData:jsondata options:0 error:&error];

    NSLog(@"%@",json);



    self.blogPostsA = [json objectForKey:@"pages"];

   }

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.blogPostsA.count;


    // Return the number of rows in the section.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CellIdentifier=@"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSDictionary *blogPost=[self.blogPostsA objectAtIndex:indexPath.row];

    cell.textLabel.text= [blogPost valueForKey:@"Title"];
    cell.detailTextLabel.text= [blogPost valueForKey:@"Author"];

sorry about the way the code got laid out its my first post :p

I formatted it for you : ). To get code to show up well in the forum you must format it using markdown. You can watch the tips for asking questions video on the right side of the page to see how to do that

the blogs arent showing up on the iphone simulator for some reason

and thank you stone

11 Answers

also it looks like you are using the wrong key for the array. pages is just a number it looks like. you want posts

self.blogPostsA = [json objectForKey:@"posts"];

thats it!! i was putting pages

it looks like you may be using the wrong keys. Looking at the raw JSON the keys are "author" and "title" not "Author" and "Title" so try this:

  NSDictionary *blogPost=[self.blogPostsA objectAtIndex:indexPath.row];

    cell.textLabel.text= [blogPost valueForKey:@"title"];
    cell.detailTextLabel.text= [blogPost valueForKey:@"author"];

so nothing is logged to the console with this line?

NSLog(@"%@",json);

Your code works fine in my Xcode.

the blogs arent popping up on the iphone simulator ?

does anything get logged to the console with your log statement?

yes the log gets the blogs and outlines them under posts

but they dont show up on the simulator

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance 0x8da3730' thats the error i get in the log

see my below post about which key you need to use for your json array. I think you are using the wrong one

ims till getting that error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance 0x8da3730'

can you post your code again with the changes oyuve made so far

oh sorry i just saw your last comment! I had to put "posts" thank you so much for seeing that !!!!!

cool glad you got it working

literally was on it for 2 hours

haha yeah that happens sometimes.