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 Basics An Introduction to Swift Programming Variables

What is the benefit in changing the contents of a variable further down? Would you not edit the original variable?

For example...

var str = "Hello"

str = "Hi"

Would you not just edit the original to be...

var str = "Hi"

What is the benefit or changing it at another stage?

5 Answers

Corey F
PLUS
Corey F
Courses Plus Student 6,450 Points

The example above has no real difference in whether you edit the original or just change it at a later stage.

You'd still get the same result.

But, then why not always work this way? We need to get used to changing data in our variables.

There is benefit in being able to alter data when we need to. Variables are meant to change in terms of what they contain.

Think of it this way.

At the beginning of a game

var highScore = 0 in your code

But based on certain conditions... Say a player scoring a point , it's good that we can change the content of the variable highScore.

Player scores and causes

highScore = 10

Otherwise we could have no game.

Thank you Corey.

This was so helpful

This was so helpful

Yusuf Alakram
Yusuf Alakram
353 Points

Was just wondering the exact same thing. Thanks Corey!