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

Jason Brown
Jason Brown
3,379 Points

In the "Build a Blog Reader" exercise, why did we create an array of dictionaries instead of just using a method call?

In the "Build a Blog Reader" exercise we created an array of dictionaries to loop through:

NSArray *blogPostArray = [dataDictionary objectForKey:@"posts"];

for (NSDictionary *blogPostDictionary in blogPostArray) {

To a novice like me this seems like an unnecessary step and it is cleaner to just use a method call within our for statement. I tried it and it worked.

for (NSDictionary *blogPostDictionary in [dataDictionary objectForKey:@"posts"]) {

I'm sure I'm missing something so I'd just like to know the reason behind the extra array of dictionaries.

Thanks!

1 Answer

Stone Preston
Stone Preston
42,016 Points

because you may want to reference that array later. Yes what you did works just fine, but if you want to reference that array further down in your code after the loop you would have to call that same method again. When you assign it to a variable initially, all you have to do is reference the variable name and not have to call [dataDictionary objectForKey:@"posts"] every time you need that array which is kind of cumbersome. its easier to just type the variable name when you need it

Jason Brown
Jason Brown
3,379 Points

Thanks Stone, I knew I was missing something, that is really helpful!