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 Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! NSArrays and For In Loops

NSArray in objective-C - please help!

Here is the question: Create a "for in" loop to iterate over drinks and print out each drink name using NSLog Please could someone give me the coding, I am pulling a complete blank here

NSArray *drinks = @[@"juice", @"water", @"coffee"];
for () {


1 Answer

Hi Kathleen,

You can iterate through an array of strings with a for loop. This requires a new local variable which handles each element of the array of strings as the code iterates through the array.

We have an array called drinks which is populated with string literals. For the sake of ease, I've created a local variable for the for loop called *drink. That will hold each element in turn as the loop moves through the array element by element.

That needs outputting using NSLog. For that, create a string literal with just a placeholder to insert another string and pass in the drink variable. That will log each element one at a time until the loop has reached the end of the array.

All that looks like:

NSArray *drinks = @[@"juice", @"water", @"coffee"];

for (NSString *drink in drinks){
  NSLog(@"%@", drink);

I hope that helps,