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

Stefan Mach
Stefan Mach
3,691 Points

Don't know how to do this

Can't do this code. I am finding that the syntax is too difficult to remember from watching a video or two so that the code challenges are too steep. I go online to look for the syntax but usually cannot find good enough examples. I am thinking about leaving team treehouse as a vehicle for learning code because I don't think they are incrementing the learning at a reasonable pace. Oh well. If someone could provide the syntax so I could move on, that would be great.

Thanks,

Stefan

nsarray.mm
NSArray *drinks = @[@"juice", @"water", @"coffee"];
Stefan Mach
Stefan Mach
3,691 Points

The code it appended is correct and not my problem. It is the code that follows in the next step.

1 Answer

Arman Arutyunov
Arman Arutyunov
21,900 Points

for in loops are pretty handy if you get how they work. the syntax is following: inside the brackets you want to tell the compiler that you need it to make some operations with each object inside the array. so you say

for(NSString *drink in drinks) {
    //here make your operations
}

you need to create an nsstring variable like you normally do with the syntax "NSString* drink". it's just a placeholder variable that represents each object of the array. it's like plain english. nothing hard to remember. it reads like "for drink in drinks make some operations" or in more natural english "create a temporary variable named 'drink' that represents each object of the 'drinks' array and for each object inside it compile following line of code {NSLog(@"%@", drink)}" this is your solution:

NSArray *drinks = @[@"juice", @"water", @"coffee"];
for(NSString *drink in drinks) {
  NSLog(@"%@", drink);
}

and my suggestion, don't leave treehouse. if you are truly eager to learn just try to get knowledge from every place it is possible. if you're stuck google, ask, check out apple documentation, stack overflow. it's hard in the beginning but you'll learn to research and find answers. just keep your chin up and everything will become possible in time. treehouse actually is a great place for beginners. always write code synchronously with teacher. if you don't get how to complete code challenge it is not a disaster. just review your code, have a rest, then rewatch the video. you'll probably find the solution. even if it is a complete dead end, as I've already said, there are always google and stack overflow online full of questions and answers.

Stefan Mach
Stefan Mach
3,691 Points

Thanks. I left for a while and watched some courses over at Lynda.com from Teacher Simon Allardice. That guy is as clear a teacher as I have found so far. Having done so, and coming back here, things are clearer.