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

Hunter Hayes
Hunter Hayes
1,415 Points

Why does the for in loop go from x-range...x+range; and similarly for y-range...y+range? This is a little unclear.

I don't understand the meaning behind the x-range and x+range? Can you clear this up for me. I am a bit confused. Thank you for your help!

4 Answers

Damien Watson
Damien Watson
27,419 Points

I'll have a crack at explaining this:

The point(x,y) is where the cannon is located on a grid and the range is how far around this point the cannon can shoot.

Assuming that range=1, the cannon can shoot 1 block to the left of its position (-1) and 1 block to its right (+1).

To calculate this, it would be from cannon.x-1 to cannon.x+1 or x-range to x+range. Breaking it down into how the for loop works it would look like something like this:

let range = 1
let cannon = Point( x:4, y:2 )

for (xCoord in (x-range)...(x+range) {}
// is the same as saying

for (xCoord in (4 - 1)...(4 + 1) {}
// loop from x position 3 (left of cannon) to 5 (right of cannon)

The y-range works the same but in the vertical range.

Does this help?

Yusuf Akbar
Yusuf Akbar
2,457 Points

Yes, helps a lot.

Thanks mate :)

it helped me to thank you