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 Build a Blog Reader iPhone App Data Modeling Using a Custom Class

J R
J R
2,037 Points

objectForKey vs valueForKey

Hi all,

I am a little confused with this part of the code on this video: BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];

Why use objectForKey and not valueForKey in this case?

1 Answer

Gabe Nadel
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Gabe Nadel
Treehouse Guest Teacher

In this case, you could actually use either valueForKey or objectForKey, they'll both work. They have some similarities and differences and there is plenty to read on the subject - I'll let you google that if you are interested.

However, on key difference is that objectForKey is actually an NSDictionary (whereas valueForKey is more general) and since the bpDictionary instance variable is of the class NSDictionary, that is the preferred one to use here.