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 Closures in Swift First Class Functions Capturing Variables

var or let

Hello everyone. Just wondering, I know it's a reference type, but just to clear some things up. What would had happened if the teacher had used a variable instead of a constant? It's the same result right? Or what is the difference?

1 Answer

Wouter Willebrands
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Wouter Willebrands
iOS Development with Swift Techdegree Graduate 13,121 Points

Hi Oscar,

It depends on the occasion what happens when a constant is changed to a variable, but in most cases this won't be a problem. However if you define something as a variable, but afterwards you are not mutating it in any way in your code, the compiler will suggest you change it into a constant (let) instead.

It is often good practice to define properties as constants, and change it to var if the compiler informs you that you're trying to mutate a constant, instead of the other way around. This prevents you from accidentally mutating a property that would allow your app to crash.

I hope this answers your question.

Kind regards,

Wouter