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

In Enemy init method, how does it get values for x and y?

In the Introduction to Classes video an init method is originally created like this:

init(position: Point) {
    self.position = position
}

I understand this just fine. But then, it is changed to this:

init(x: Int, y: Int) {
    self.position = Point(x: x, y: y)
}

What I don't understand is where this init method is getting the values for x and y when creating a new Point. The Enemy class has no such values! What am I missing?

The same question occurs in the init method for the Tower class in the next video. I just don't understand where the init method is getting the values for x and y that it uses when initializing position.

Help me Obi Wan Kenobi! You're my only hope!

3 Answers

Jose Patarroyo
Jose Patarroyo
2,953 Points

Hello Steve,

As you will see in the next set of videos, the x and y letters in the init method, are just the names associated with the Enemy class when you call it and initialize it within your code. In others words they are just an easy way to let you know what kind of values the class is expecting.

In the video, Passan uses x and y to initialize the Enemy class, because when you create an enemy it will ask for it's position and a XY coordinate is widely known.

Finally, the values for X and Y are going to be given every time you call the Enemy class or Tower class to create a new enemy or tower, that's where the values will come from.

Hey Jennifer Nordell

do you have any explanation for this as Im still not understanding it fully. I get the original way of creating the init method but like Steve Graff said I don't understand where the x and y coordinates are coming from exactly, the only explanation I can think of is the Point Struct created earlier, but again the properties within that struct are supposed to be limited to that struct so I'm confused.

Thank you for your patience Alia

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi everyone! This is sort of an old thread, but since I was specifically tagged I will try to give an explanation. I actually found Jose Patarroyo 's answer quite good.

Right now, those x and y values don't exist really. That's because we have yet to create an instance of Enemy. However, upon creation of the Enemy we will pass in integers which the init method will use to create a new Point or position. When that happens the integers will be assigned to the values x and y inside the init method which will result in creating the position for that enemy. The position of is of type Point which we defined in a struct.

So what we're really saying is this: when I create an Enemy I'm going to send in two numbers. Use those two numbers for the coordinates and assign them to the values x and y respectively.

Hope this helps! :sparkles: