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
Richard Lu
20,185 PointsGathering data with JSON
When I use the google JSON API, the format given is not similar to the one on TreeHouse.
for TreeHouse we have:
'''json { ... ... -post: [...] } '''
But for the Google JSON search API we have:
'''json -responseData: { -results: [ { ... } ] ... ... } '''
for the Google search API we have the results nested by responseData which is giving me a runtime error:
unrecognized selector sent to instance
and it points to this line of code:
NSDictionary *blogPosts = [self.blogPosts objectAtIndex:indexPath.row];
I've implemented to code to fetch the information like this:
NSURL *blogUrl = [NSURL URLWithString:@"https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=richard"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogUrl];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.blogPosts = [dataDictionary objectForKey:@"responseData"];
How would I be able to get around that?
4 Answers
Stone Preston
42,016 Pointshmm what I think is happening is that your are setting your blogPosts property to this:
self.blogPosts = [dataDictionary objectForKey:@"responseData"];
however that responseData key is another dictionary, not an array as you can see here:
responseData: { -results: [ { ... } ] ... ... }
its got keys and values inside it. so your self.blogPosts property now holds a dictionary, which would be why it does not respond to the objectAtIndex method (which is called on arrays, not dictionaries) and why you are getting that unrecognized selector error.
To remedy this, you would need to set your self.blogPosts property to an array thats within that responseData dictionary.
Richard Lu
20,185 PointsThanks, I took your advice and I saved the dictionary onto a new dictionary thats within the responseData dictionary:
Incase anybody else encounters this issue in the future here's what I did:
dataDictionary = [dataDictionary objectForKey:@"responseData"];
self.blogPosts = [dataDictionary objectForKey:@"results"];
instead of just doing:
self.blogPost = [dataDictionary objectForKey:@"responseData"];
Akib Quraishi
2,306 PointsStone Preston & Xing = My Code is Running... [self.thanks]
Joan Guerra Makaren
4,067 PointsExcellent solution Richard Lu i dit and worked for me too, now the biggest issue is that those search results only shows the first 4 results