Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Complex Data Structures Methods

I need clarification on this loop. I don't quite understand how the for loop iterates over 3 x and y values.

I understand the for loop uses the closed range operator to keep it within a certain value: for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange { for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange { let coordinatePoint = Point(x: xCoordinate, y: yCoordinate) results.append(coordinatePoint) } } However I don't EXACTY understand why these values eventually come to an end. Also if one could clarify the significance of this line: let coordinatePoint = Point(x: xCoordinate, y: yCoordinate) that would be a huge help.

Thanks!

Here is the complete line of Code:

let coordinate1: (x: Int, y: Int) = (0,0) coordinate1.x

struct Point {

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

}


let x: Int // these *stored properties* have no value assigned to them - that comes later
let y: Int // their job is to name the constant, and define its type.

/// Returns the surrounding points in range of
/// the current one
func points(inRange range: Int = 1) -> [Point] {    // Here we are creating a new function within the struct, with a default value of 1
    // We have then specified the return type as being an array of Point instances

    var results = [Point]() // Here we declare a new variable of type Point, and assign an empty array. We'll eventually assign
    // coordinates to this array. It's a variable because we need to mutate it throughout this process.

    let lowerBoundOfXRange = x - range // get the lower bound numbers of the origin x point and assign them to a constant
    let upperBoundOfXRange = x + range // get the upper bound numbers of the origin x point and assign them to a constant

    let lowerBoundOfYRange = y - range // get the lower bound numbers of the origin y point and assign them to a constant
    let upperBoundOfYRange = y + range // get the upper bound numbers of the origin y point and assign them to a constant

    /*
     now we start a loop which will go through all of the x values and y values and create a new constant containing the values based on the custom type Point. this constant is then added (appended) to the above "results" variable, and the loop continues until all points have been collected and added to the results array.
     */
    for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {
        for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {
            let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
            results.append(coordinatePoint)
        }
    }

    return results // finally, the function returns the results array ready for calling back later
}

}

4 Answers

Shakal Uddin
Shakal Uddin
965 Points

For the first part of your question: Recall this part

let lowerBoundOfXRange = x - range
let upperBoundOfXRange = x + range 

let lowerBoundOfYRange = y - range 
let upperBoundOfYRange = y + range 

You're basically setting the first number and last number that the loop needs to go through to store into the array.

For example, lets say hypothetically, if a user enters the points (2,2) for the struct, and the default value "range" is set to 2. Then the lower bounds is 0 and the upper bounds is 4 because you are essentially subtracting 2 and adding 2 to achieve the lower and upper bounds for X and Y respectively. So we will create a "for in" inside of another "for in" loop that will loop through 0,1,2,3,4 for X and Y to gather as many combinations as we can within those range of numbers.

for xCoordinate in lowerBoundOfXRange...upperBoundOfXRange {

     for yCoordinate in lowerBoundOfYRange...upperBoundOfYRange {

            let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
            results.append(coordinatePoint)

        }

}

So visually the loop is going like this, (0,0), (0,1), (0,2), (0,3), (0,4), (1,0),(1,1),(1,2) ... and so forth. Notice that the X value increments by 1 once Y becomes 4. Then the Y value starts over to work its way back to 4 so then X increments by 1 again and Y starts over again all the way until the loop stops reaches both upper bounds (which will be (4,4)). It's a neat way to gather all of the combinations possible.

For your second question: You're storing the X and Y combinations you found through the loop into an "instance" of the Point struct for each combination created. You accomplish this by appending each combination into an array at the end of loop. So we’re appending (X, Y) (because of the Point struct)

let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)

The array will look like [(0,0),(0,1),(0,2)...]

Thank you so so much!!!

NAVEED CHOWDHURY
NAVEED CHOWDHURY
1,142 Points

I guess I am still confused with the logic part of it. I understood the looping part. The part which is still foreign to me is the "Methods" part were you are passing in the xCoordinate Values and yCoordinate Values. It seems like we are passing in values like arguments in a function. But then again the values are just being stored to be appended in the results array. Maybe I need to read up more on Methods and its properties.

Otto linden
Otto linden
5,857 Points

Why does it go like this: (0,0) (0,1) (0,2).... and not like this: (0,0) (1,1) (2,2)....