Skip to content

Instantly share code, notes, and snippets.

@hansott
Created November 9, 2014 12:22
Show Gist options
  • Save hansott/a50f11dd106bf82f83ad to your computer and use it in GitHub Desktop.
Save hansott/a50f11dd106bf82f83ad to your computer and use it in GitHub Desktop.
2D Grid for objects in Swift
struct Grid<T> {
var array : Array<Array<T>>
let rows : Int
let columns : Int
init(rows : Int, columns : Int, initialValue: T) {
self.rows = rows
self.columns = columns
self.array = Array<Array<T>>()
for row in 0...rows {
array.append(Array(count: self.columns, repeatedValue: initialValue))
}
}
subscript(row : Int, column : Int) -> T {
get {
return self.array[row][column]
}
set(value) {
self.array[row][column] = value
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment