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

Photo bombers: get the number of likes and set it to the button title

// Number of likes
    NSString *likes = [_photo valueForKeyPath:@"likes.count"];
    [self.likesButton setTitle:likes forState:UIControlStateNormal];
    NSLog(@"%@", likes);

I can log the number of likes, but when i set is to the button title i get the following error:

-[__NSCFNumber length]: unrecognized selector sent to instance 0x8f1b430

2 Answers

are you sure that the "likes.count" returns a string and not an NSNumber? try using this perhaps:

NSNumber *likesNumber = [_photo valueForKeyPath:@"likes.count"];
NSString *likes = [likesNumber stringValue];
[self.likesButton setTitle:likes forState:UIControlStateNormal];

Thanks! I already thought it was an NSNumber. Just didn't know how to convert it to an NSString.

NSString *likesCount = [[NSString alloc] initWithFormat:@"%@", _photo[@"likes"][@"count"]]; [self.likesButton setTitle:likesCount forState:UIControlStateNormal];

works to for me what would be the difference