我想在swift(二维)中创建一个包含每个字段上的CLASS(groundType,height,visible等)的地图。
没有发现如何重新整理这个...大多数示例只有简单的Int或Boolean作为内容。
我可以访问:
let currentField = field[x,y].height()
有人可以帮忙吗?
答案 0 :(得分:0)
以下是创建5 X 3
多维数组以保存Field
类型的示例:
class Field {
init (groundType: String, height: Double, visible: Bool) {
self.groundType = groundType
self.height = height
self.visible = visible
}
var groundType: String
var height: Double
var visible: Bool
}
// A two-dimensional array is just an array of arrays. Use the Array constructor
// twice to create a 5 by 3 multidimensional array
var fields:[[Field?]] = Array(count: 5, repeatedValue: Array(count: 3, repeatedValue: nil))
fields[4][2] = Field(groundType: "mountain", height: 4300.23, visible: true)
if let height = fields[4][2]?.height {
println(height)
}