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

Are there restrictions on when you can instantiate a struct/class within a method of itself?

In the video for methods in Swift we created an array of type Point inside the Point Struct. I am not understanding how we are able to create an array of type Point named results but cannot create a variable (var Example) of type Point inside that same method. Is the array acting as a data model to check x and y? If so why not just create a struct outside of the Point struct to prevent confusion? (Also note that in the video x and y are constants, I changed them to variables for this example.)

struct Point {
    var x: Int
    var y: Int
    func surroundingPoints(withRange range: Int = 1) -> [Point] {
        var results: [Point] = []

       /*This section executes for a short while before Xcode throws an error message off Playground execution aborted: Execution was interrupted, reason: EXC_BAD_ACCESS (code=2, address=0x7fff596e4ed8).
*/
        var Example = Point(x: 0, y: 0)
        Example.x = 5
        Example.surroundingPoints()

        for xCoord in (x-range)...(x+range) {
            for yCoord in (y-range)...(y+range) {
                let coordinatePoint = Point(x: xCoord, y: yCoord)
                results.append(coordinatePoint)
            }
        }
        return results
    }

}

2 Answers

Hi Freddy,

The empty array is initialized inside the function. Not inside the struct. A function can have multiple internal variables. That is why when you initialize the struct, it does not initialize an empty array. The empty array only gets created when the function is called and is not available outside of the function.

Also, an empty array is a thing. It is a list with count zero. An empty variable would be nil so they are not the same thing.

Here is an example. If we wanted to have a struct with a results array, then we would need to create a custom initializer. In this hypothetical example, I have just added the existing point into the array. You could also write the surroundingPoints function inside the initializer to add all of the surroundingPoints.

struct Point {
    var x: Int
    var y: Int
    var results: [Point] = []
    init (x: Int, y: Int) {
        self.x = x
        self.y = y
        results.append(self)
    }
}

let myPoint = Point(x: 5, y: 5)
myPoint.results.count

I hope that helps to clear things up a little bit.

Also, your code works perfectly if you remove the example lines from your code.

struct Point {
    var x: Int
    var y: Int
    func surroundingPoints(withRange range: Int = 1) -> [Point] {
        var results: [Point] = []

        for xCoord in (x-range)...(x+range) {
            for yCoord in (y-range)...(y+range) {
                let coordinatePoint = Point(x: xCoord, y: yCoord)
                results.append(coordinatePoint)
            }
        }
        return results
    }

}

let myPoint = Point(x: 5, y: 5)
myPoint.surroundingPoints()

Mike

PS: Thanks to the awesome resources on Team Treehouse, I just launched my first app. :-)

Code! Learn how to program with Swift

When you create an array of type Point inside the function, is it only initializing the variables x and y only?

I added some additional comments that might help to clear things up.

struct Point {
    var x: Int
    var y: Int
    func surroundingPoints(withRange range: Int = 1) -> [Point] {
        var results: [Point] = [] // Initializing empty array
        results.count // Will return zero since you have an empty array

        for xCoord in (self.x-range)...(self.x+range) { // range loop based on x coordinate of point
            for yCoord in (self.y-range)...(self.y+range) { // range loop based on y coordinate of point
                let coordinatePoint = Point(x: xCoord, y: yCoord) // Creating new points based on initial point
                results.append(coordinatePoint)
            }
        }
        return results
    }

}

let myPoint = Point(x: 5, y: 5)  // point with x and y coordinate is initalized here not inside the function
myPoint.surroundingPoints()
Nathan Tallack
Nathan Tallack
22,164 Points

Your Struct works perfectly if you remove the three lines of Example from the within the method.

The looping you describe only occurs with that Example code in there because you are recursively calling the method from within it because you have placed those declarations inside.

I don't know which video you are talking about because you did not link to it in your course so I am not sure what you are trying to do with that Example declaration inside the method.

Perhaps link to the video so I can see what you are intending to do?

My concern is how is it possible to initialize an empty array of type Point inside the Point struct but cannot do the same with a variable.

https://teamtreehouse.com/library/objectoriented-swift-20/complex-data-structures/methods