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 Swift 2.0 Enumerations and Optionals Objects and Optionals Enumerations With Raw Values

Kevin Gutowski
Kevin Gutowski
4,082 Points

Unsure about for loop

I'm a but confused about the for loop below (as in the video)

for coin in coins {

//loop here

}

What exactly does coin do? I don't recall going over this in the other swift videos. It feels like coin is just a dummy name which I can define as a or i and it would work just fine.

1 Answer

Taylor Amsler
Taylor Amsler
4,427 Points

Hi Kevin. That is exactly correct, coin is a dummy value. If you do not use it in the for-in loop Swift will most likely tell you to use a _ as a neutral placeholder. You absolutely could use a or i or whatever you wanted, but the dummy value IS necessary.

The purpose of the dummy value is to represent each individual value in the list. So coin here represents each value (each coin) in the values (all the coins) you have in your list. This is useful because you probably want to perform a different action for different individual values. For example your for-in loop might add a dime as +10 whereas a quarter might be +25.

Hope this was a clear explanation, I struggled with this for a bit too.

Kevin Gutowski
Kevin Gutowski
4,082 Points

Thanks mate! I appreciate the response! Sounds like I should't think about it too hard :D