Reputation: 213
I have a 2-dimensional 5x5 array in Swift. I am trying to have each array item to present a struct that has properties like cost and heuristics. for example, the grid[0][0]
item should have cost and heuristics values.
Swift implementation:
struct Spot {
var cost: Int // cost
var heu: Int // heuristics
}
var grid = [[Int]]
In Javascript I used to do it as:
function Spot() {
this.cost = 0;
this.heu = 0;
}
//This is what I'm looking for something equivalent in Swift
grid[0][0] = new Spot();
Sorry if this seems very basic, but I'm a beginner in Swift.
Upvotes: 0
Views: 68
Reputation: 236260
You need an array of Spot
arrays [[Spot]]
, not an array of Int
arrays [[Int]]
.
struct Spot {
let cost: Int // cost
let heu: Int // heuristics
}
var grid: [[Spot]] = .init(repeating: .init(repeating: .init(cost: 0, heu: 0), count: 5), count: 5)
grid[0][0] = .init(cost: 10, heu: 5)
print(grid) // "[[Spot(cost: 10, heu: 5),...
print(grid[0][0].cost) // 10
print(grid[0][0].heu) // 5
Upvotes: 3