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 Object-Oriented Swift 2.0 Classes Introduction to Classes

Sacha Karsenty
Sacha Karsenty
3,903 Points

Why use a contant for position and not a variable? And does creating an instance of point before creating an instance of

So I understand the fundamental difference between contants and variables. One is a is immutable (it's value can't change) and the other mutable (it's value can change).

Pasan describes life of the enemy to be a variable because every time an enemy gets hit it will lose "life" so it changes it's not fixed. However he makes the position a constant, why? Because the position of the enemy can change, at least I perceive it so. If they never move the game has no point right?

Second question, Pasan shows that by giving x, y parameters to the init method in the class. The reason is that that way when we create an instance of enemy we do not have to create an instance of point before. I don't really understand that since we create an instance of point within the init method anyways, or do I get that completely wrong?

Thank you for the help and clarification.

2 Answers

Min Choi
Min Choi
1,517 Points

Hey Sacha! I'm taking the same course as you are and I was asking the same question myself for the constant but I think I have the answer for your second question. So when we create an instance of Point, it exists globally for later use as well. However, if we just input x and y, we create an instance of point within the class. This means that the point within the class is only local and does not exist globally due to the scope of the function.

Sacha Karsenty
Sacha Karsenty
3,903 Points

Hi Min,

Thank you for your answer, it makes sense and it makes sense as well when you actually calling the class so you don't have to make an instance point before creating an instance of the class.

I actually have the answer the first question I think. I saw the next video and he seems to confirm what we say that it should probably be a variable.

Min Choi
Min Choi
1,517 Points

Yeah Pasan has done that in the past where he fixes or changes things in later videos so I assume he will change the constant later on.