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 Complex Data Structures Methods

(x-range)...(x+range)

Watching the swift video and I am still confused how this works i know that it produces the desired result that the guy is trying to do. but how and what is it doing. in my head it doesn't make sense that line of code is read to me as. x which is zero defined in the struct which zero is the default value. so zero minus 1, which is the value given in the function. so zero minus one is -1. 0 plus one is 1. so the range in between these values is 0 - 1 and since they aren't double values they aren't infinite. I don't know anymore can someone explain this and dumb down the explanation.

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hey Devin,

The code produces a list of points around the 'coordinatePoint' which are available based on the cannons range. In the example you provided above and what he has on the screen, he has set them to (0,0). But these could be set to anywhere on the grid.

With your example of (0,0), on the x, it would return the range [-1, 0, 1]. As it is starting from '0' and ranges from minus 1 to plus 1. If the cannon was to move to (2,2) then the range would be [1, 2, 3] as he has shown.

In the video he keeps saying (2,2) so he should really have the following line so it matches with what he's talking about. This may be what you're finding confusing.

// initial line 12 should be:
let coordinatePoint = Point(x: 2, y:2)

Does this help?

yes, it took me a while but the code and what he was saying was confusing me. I was working ahead and was seeing that his math was wrong for 0,0 and not listening closely that he was talking about the points 2,2 thank you for the answer. If you could answer another quick question that would be great. In the next couple of videos, he starts to go over init methods. I began to wonder why we use these if they are already assumed in the compiler doesn't it seem redundant to write code that has already been written. My question really is, what is the purpose of an initializer method.

Damien Watson
Damien Watson
27,419 Points

Most languages that are Object Oriented (OOP) use an initializer method to define certain parameters when you create a new class. The example in this video uses struct vs class.

In a class, you usually don't define any properties directly and usually pass them in on creation, this uses an initialize method. So if his Point example was a class instead of struct, it would look something like this:

class Point {
  var x: Int
  var y: Int
  init(x: Int, y: int) {
    self.x = x
    self.y = y
  }
}

var point = Point(x: 2, y: 2)

This just means that you can control how the class is used and defined.