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

Why does my code not return the correct coordinates in the array when the method is called?

struct Point {

    let x: Int
    let y: Int

    ///Returns the sourounding pointds in range of
    ///the current one
    func points(inRange range:Int = 1) -> [Point] {
        var results = [Point]()

        let lowerBoundofXRange = x - range
        let upperBoundofXRange = x + range

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

        for xCoordinate in lowerBoundofXRange...upperBoundofXRange {
            for yCoordiate in lowerBoundOfYRange...upperBoundOfYRange {
                let coordinatePoint = Point(x: xCoordinate, y: yCoordiate)
                results.append(coordiantePoint)
            }

        }

        return results

    }
}

let coordiantePoint = Point(x: 0, y: 0)

coordiantePoint.x
coordiantePoint.points()

2 Answers

for xCoordinate in lowerBoundofXRange...upperBoundofXRange {
        for yCoordiate in lowerBoundOfYRange...upperBoundOfYRange {
            let coordinatePoint = Point(x: xCoordinate, y: yCoordiate)
            results.append(coordinatePoint) // Spelling error your error is right here // 
        }

spelling error

looks like you’re missing the struct object.