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 Basics (retired) Control Flow While and Do-While Loop

Can you have multiple arrays? If so, I'm confused how you can create a new variable that knows which array to pull from?

Can you have multiple arrays? If so, I'm confused how you can create a new variable that knows which array to pull from?

Specifically referring to when we added this line: var index = 0

Thanks!

2 Answers

Donald Test
Donald Test
3,339 Points

Yes, you can have multiple arrays. Since each array should have a different name, each will be referenced separately. Also, "index" in the case you're talking about is arbitrary. You could write i or whatever you like. As long as you initialize the variable and use different variables for each array (when incrementing for the loop, that is), you should be fine.

I think that makes sense. So if I had 2 arrays, and then created a variable to reference the first array only, how would that look? Would it be something like var index = array1[0]?

Donald Test
Donald Test
3,339 Points

Yep, that's exactly it. For your other array it would be beneficial to use a different variable or constant (depending on what you require). Of course, depending on the purpose, you could use the same variable to reference different arrays. It all comes down to what functions you want to the code to complete.

Awesome. Thanks!