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

Soon Chang
Soon Chang
5,291 Points

Declaring constant inside of for loop and outside of for loop

Hello,

I have a question regarding the following two codes because one works, and another one does not.

Working Code

var results: [Int] = []

for multiplier in 1...10 {

let multiplier = multiplier * 6

results = results + [multiplier]

}

Non-Working Code

var results: [Int] = []

for multiplier in 1...10 {

let multiplier = multiplier * 6

}

results = results + [multiplier]

If you declare a constant inside of loop, is it not recognized outside of for-loop?

Thanks for your help!

3 Answers

Jake Adams
Jake Adams
1,608 Points

I don't think "closure" is the correct term here. In Swift, a closure is a self contained block of code that can be passed around much like a variable (like blocks in Objective-C).

In this particular case, the reason the constant is not available outside the loop is one of scope. The loop defines a new scope in the compiled code and only the code within that loop has access to variables and constants that are defined on that scope.

The code within the loop scope does have knowledge of the scope outside (the parent scope), but the parent doesn't know what the child is doing :) That's why a variable declared outside a loop can be accessed within the loop.

One the clearest explanations I've read on this subject came from a JavaScript developer and he has a set of open source books on GitHub: You Don't Know JS. I highly recommend reading that even as an iOS developer. It's quick and easy and very clear.

Thanks for the book recommendation! I skimmed the sections on scope, as it isn't very well defined in the Swift documentation. I'd thought that any section of code surrounded by { } is a closure in Swift, and that the closure was capturing variables from its context (as described in the Swift documentation on closures); are you saying that that is incorrect, or just that scope is a better way to describe what's going on?

Jake Adams
Jake Adams
1,608 Points

The concept behind a closure is that a code block { } encloses a reference to a variable defined in the parent scope for use at a later time. An example would be a callback function that we pass to another class or function. That closure keeps the variable allocated and modifies the original value that the reference points to.

A simple code block like a for..in loop wouldn't be referred to as a closure because it's all executed inline.

All that being said it may technically be treated as a closure by the compiler, but the underlying concept here is scope, where variables are declared, and how parent/child scopes interact. I would avoid referring to a loop (or any inline code block) as a closure because that term has a specific meaning in Swift.

Hope that helps! And of course, take what I say with a grain of salt, not gospel, as I am by no means a Swift expert :D

Soon Chang
Soon Chang
5,291 Points

Thank you for the detailed explanation!

Hello Soon,

In short, the answer to your question is "yes". However, it's not the fact that it's a for-loop that's important, it's the fact that it's a closure. If you were to declare multiplier outside the loop, like you did results, then it could be altered within the loop and then used outside again.

Hope that helps!

Soon Chang
Soon Chang
5,291 Points

Hi Evan,

Thanks for your reply! Ah, indeed, if you declare something inside the closure, it is only available inside the closure, which makes sense.

Thank you!

Soon