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 Collections and Control Flow Control Flow With Loops For In Loops

Meedo Al
seal-mask
.a{fill-rule:evenodd;}techdegree
Meedo Al
iOS Development Techdegree Student 2,203 Points

Regarding For-in-loop Index Variable

it was mentioned in the code challenge that we should define a constant called multiplier, to store temporary value of the multiplication result in each cycle of the loop, I find it completely confusing that it's supposed to be constant but for temporary value, besides there is no way to change classify it explicitly as a variable with the word var or as a constant with the word let.

is it by the language author meant to be always constant?

1 Answer

var results: [Int] = []

for multiplier in 1...10 { var multiple = multiplier * 6

results.append(multiple)

}

The "multiplier " constant in the for loop acts as a temporary constant. That just means that for every time through the loop the current number being worked on doesn't get mutated and reassigned to "multiplier". If that was the case, then compiler would throw an error. each time through the loop the multiplier constant is initialized. (different from mutating- think if I wrote "let multiplier = 10" then decide to change 10 to 15 as an initializer.) after the variable "multiple" gets assigned the "multiplier * 6" value, the constant "multiplier" still has the same value, until its reinitialized on the next iteration through the loop.