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

jon kelson
jon kelson
5,149 Points

withRange range

Hi all, I'm getting a fault message with the perameters local and external for func surroundingPointsWith.. I've just writen this code exact I think from the video and he had no faults . its telling me i should not have the withRange and i should delete it to fix the problem.

struct Point { let x: Int let y: Int

func surroundingPointsWith(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 coordinatePoint = Point(x: 0, y: 0) coordinatePoint.x

1 Answer

The problems are (1) a missing curly brace and (2) a reversed curly brace:

struct Point {
    let x: Int
    let y: Int

    func surroundingPointsWith(withRange range: Int = 1) -> [Point] {
        var results: [Point] = []
        for xCoord in (x-range)...(x+range){
            for yCoord in (y-range)...(y+range) { //missing {
                let coordinatePoint = Point(x: xCoord, y: yCoord)
                results.append(coordinatePoint)
            } //need } instead of {
        }
        return results
    }
}

let coordinatePoint = Point(x: 0, y: 0)
coordinatePoint.x
jon kelson
jon kelson
5,149 Points

Thanks I amended the curly braces but that didn't solve the problem . For some reason it didn't like the "with" part of the withRange . I tried it with lots of other Double words such as allRange etc and it worked fine . Haven't a clue why .

The errors saying 'with' implied for the first part of the method; did you mean to name this parameter 'range'.

Ok ! Just after writing the above I just deleted the space in between withRange and range and then put the space back in . It works fine now . Very strange !!!