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 Instance Methods

Boris Likhobabin
Boris Likhobabin
3,581 Points

Not exactly surrounding points! The results of this method also include the point itself.

If lets say we pass on to the method x:2, y:2 as an output we get point (2,2) as well which is the point that were not looking for as it is not the one surroundING but the one that is surroundED. I thought what we could do is take that point out of the array before the results are printed? however I can't find something which is the opposite of append.

Can you please tell me whether I am wright with my suggestion or did I not get what Pasan tried to accomplish with this method?

Thanks in advance By the way this is my first question that I ever post about coding (not just teamtreehouse) so I'm super excited about the answer!

1 Answer

You can try this to get the output without the original point "(2,2)".

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

    for xCoordinate in (x-range)...(x+range){
        for yCoordinate in (y-range)...(y+range) {
            let coordinatePoint = Point(x: xCoordinate, y: yCoordinate)
            if ((coordinatePoint.x == originalPoint.x) && (coordinatePoint.y == originalPoint.y)) {

            } else {
                results.append(coordinatePoint)
            }
        }
    }
    return results
}