Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Collections and Control Flow Control Flow With Loops Working With Loops

Sean Lafferty
Sean Lafferty
3,029 Points

Dont get it, yet again. This is very difficult to grasp!

Just dont understand what it means when retrieve the values from the array

loops.swift
let numbers = [2,8,1,16,4,3,9]
var sum = 0
var counter = 0

// Enter your code below

while counter < numbers.count {
  counter += 1 
  newValue = numbers.count(sum += newValue)


}

1 Answer

Jack Baer
Jack Baer
5,049 Points

Basically, the array is the list of values inside of the square brackets []. When you declare one, each value inside (separated by commas), is given a numerical placement. The first value is at place 0, the second at place 1, and so on. You retrieve data from an array by calling the array's name, and the value's placement number. For example, if I wanted to get the value 16 from the array numbers, since it is in place 3, I would create a new variable and assign 16 to it like so:

let someArrayValue = numbers[3]

This is setting 16, the value from the array, to someArrayValue. It knows how since I put 3 in square brackets after the array name, which is the place number. If I were to change [3] to [2], then someArrayValue would be 1, as that is the value in place 2.

And also when you see the array name with .count after that, it gives the number of values inside the array. So if I had an array with 10 values in it, and used .count on the array to set a value to a variable, then I would get the number 10 assigned.

If you have any more questions, feel free to ask me.

Hope this helps! ~Jack