Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Stage 3 Photo Bombers - Unrecognized Selector Sent to Instance JSON Response

I have this code that shows the exception breakpoint when I run my Photo Bombers app:

NSLog(@"Photo: %@", _photo);
NSURL *url = [[NSURL alloc] initWithString:_photo[@"images"][@"thumbnail"][@"url"]];

This is the error I am getting:

-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x10e9b6890

The error always shows up when I try to parse the JSON response. I thought this error meant I was sending the wrong type to the instance of NSURL But as you can see from the results of my NSLog statement above it is a string that is being returned (which is what initWithString is expecting). I am at a loss at this point.

The is the NSLog statement results:

Photo: http://scontent-b.cdninstagram.com/hphotos-prn/t51.2885-15/10387948_1416512958627949_673063313_n.jpg

6 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

on line 66 in TTPhotosViewController write this instead:

        self.photos = [responseDictionary valueForKeyPath:@"data"];
Gareth Borcherds
Gareth Borcherds
9,372 Points

So the error is related to NSDictionary problems. You can see a similar question here at stack overflow: http://stackoverflow.com/questions/21268539/nsarraym-objectforkeyedsubscript-unrecognized-selector-sent-to-instance

What doesn't make sense to me is when you use NSLog your _photo instance variable returns a string, so why wouldn't you simply pass _photo to your initWithString parameter instead of _photo[@"images"] etc. it appears that from the error _photo is a MSString object not a NSDictionary object like you're treating it. I can't quite tell though with just this code snippet, but I think that's right.

Thomas Nilsen
Thomas Nilsen
14,957 Points

If you can zip the project and provide a dropbox link or something like that, I think we'll be able to help you spot the error more easily :)

Here is a link to the GitHub project. I wonder why Treehouse puts the files in zipped format instead of just a link to the project on GitHub at each stage of completion.

https://github.com/mattcantstop/photo_bombers

Anyway, thanks for the help!

"What doesn't make sense to me is when you use NSLog your _photo instance variable returns a string, so why wouldn't you simply pass _photo to your initWithString parameter instead of _photo[@"images"] etc."

- (void)setPhoto:(NSDictionary *)photo {
    _photo = photo;
    NSLog(@"Photo: %@", _photo);
    NSURL *url = [[NSURL alloc] initWithString:_photo[@"images"][@"thumbnail"][@"url"]];
    [self downloadPhotoWithURL:url];
}

In the line that is throwing the error I am setting the URL variable to the string that is the "url" attribute of the JSON response. I then package up that url value as the destination to download the image from the NSString url location. Let me know how that understanding is wrong, if you can please.

Thanks Thomas! I had tried that in the setPhoto line but had not tried that there.