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

Kjetil Korsveien
Kjetil Korsveien
1,713 Points

I get a lot of errors in Xcode.

I get errors in the code in Xcode following this example. Cant figure out what is wrong?

Kjetil Korsveien
Kjetil Korsveien
1,713 Points

struct Point { let x = Int let 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 coordinatePoint = Point (x: 0, y:0) coordinatePoint.surroundingPoints()

4 Answers

Felix Ayala
Felix Ayala
5,541 Points

Hi Kjetil!

I see many syntax errors in your code. First of all, you need to declare Struct properties like this:

// Do
let x: Int
let y: Int = 0 // Here we are assigning an initial value

// Don't
let x = Int

Inside your function are ocurring the same:

// Do
var results:[Point] = []

// Don't
var results = [Point] = []

Everything should look like this:

struct Point {
    let x:Int
    let 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 coordinatePoint = Point (x: 0, y:0)
coordinatePoint.surroundingPoints()

Hope it helps!

Felix

Kjetil Korsveien
Kjetil Korsveien
1,713 Points

Tnx a lot :) and how do I post my code like you do?

I still get an error on the return results function.

struct Point { let x: Int let y: Int

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

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

Felix Ayala
Felix Ayala
5,541 Points

In your code there is an extra dot:

return.results // Wrong
return results // Correct

Regards

Kjetil Korsveien
Kjetil Korsveien
1,713 Points

Not in my latest post. I corrected a lot, but I still get an error on return results . I pasted your code and it worked. Has it anything to say that I use a Norwegian keyboard?

Felix Ayala
Felix Ayala
5,541 Points

I corrected a lot, but I still get an error on return results . I pasted your code and it worked.

What did the error says?

Has it anything to say that I use a Norwegian keyboard?

I don't think so.

How do I post my code like you do?

You can learn Markdown Syntax. Click the link Markdown Cheatsheet below the form.

Regards

Felix