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
miles fishman
Courses Plus Student 3,610 PointsI 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"];
Stone Preston
42,016 PointsI 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
miles fishman
Courses Plus Student 3,610 Pointsthe blogs arent showing up on the iphone simulator for some reason
and thank you stone
11 Answers
Stone Preston
42,016 Pointsalso 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"];
miles fishman
Courses Plus Student 3,610 Pointsthats it!! i was putting pages
Stone Preston
42,016 Pointsit 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"];
Stone Preston
42,016 Pointsso nothing is logged to the console with this line?
NSLog(@"%@",json);
Soojin Ro
13,331 PointsYour code works fine in my Xcode.
miles fishman
Courses Plus Student 3,610 Pointsthe blogs arent popping up on the iphone simulator ?
Stone Preston
42,016 Pointsdoes anything get logged to the console with your log statement?
miles fishman
Courses Plus Student 3,610 Pointsyes the log gets the blogs and outlines them under posts
miles fishman
Courses Plus Student 3,610 Pointsbut they dont show up on the simulator
miles fishman
Courses Plus Student 3,610 PointsTerminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance 0x8da3730' thats the error i get in the log
Stone Preston
42,016 Pointssee my below post about which key you need to use for your json array. I think you are using the wrong one
miles fishman
Courses Plus Student 3,610 Pointsims till getting that error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance 0x8da3730'
Stone Preston
42,016 Pointscan you post your code again with the changes oyuve made so far
miles fishman
Courses Plus Student 3,610 Pointsoh sorry i just saw your last comment! I had to put "posts" thank you so much for seeing that !!!!!
Stone Preston
42,016 Pointscool glad you got it working
miles fishman
Courses Plus Student 3,610 Pointsliterally was on it for 2 hours
Stone Preston
42,016 Pointshaha yeah that happens sometimes.
miles fishman
Courses Plus Student 3,610 Pointsmiles fishman
Courses Plus Student 3,610 Pointssorry about the way the code got laid out its my first post :p