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
Aaron Moreno
7,298 PointsMy availablePositions instance isn't showing up like an array. Why is that?
//Beginning
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){
var results: [Point] = []
for xCoord in (self.x-range)...(self.x+range){
for yCoord in (y-range)...(y+range){
let coordinatePoint = Point(x: xCoord, y: yCoord)
results.append(coordinatePoint)
}
}
}
}
//at the end of my code...The instance that I'm assuming should be an array
let availablePositions = position.surroundingPoints(withRange: range)
2 Answers
Ali Amin
1,295 PointsYeah sure, you forgot to add this too to function.
-> [Point]
It should be like this:
func surroundingPoints(withRange range: Int = 1) -> [Point] {
...
}
Also range and position should be something like this. (position should be of type Point and range should be an Int)
let position = Point(x: 1, y: 2)
let range = 2
Ali Amin
1,295 PointsYou forgot to add return results at the end of the func
Aaron Moreno
7,298 PointsThanks for the response Ali, even after I add return results after the second bracket in the surroundingPoints method, it gives me an error at that return value and still on the availablePositions instance
Aaron Moreno
7,298 PointsAaron Moreno
7,298 PointsYou're amazing Ali! Thank you!!
Ali Amin
1,295 PointsAli Amin
1,295 PointsYou are welcome