我想声明一个数组,它应该是这样的:
myArray
->Car (Array)
- Ford
- Audi
- ...
->Color (Array)
- Red
- Green
- ...
->Wheels (Simple String, eg "4")
->Roof (Simple String, eg, "NO")
我在声明和访问这种类型的数组时被卡住了。谢谢你的帮助!
我认为创建自定义对象(带有类或结构)将更适合您,例如:
class Car {
var brand: String?
var color: String?
var wheels: Int?
init(brand: String, color: String, wheels: Int = 4) {
self.brand = brand
self.color = color
self.wheels = wheels
}
}
let myAudi = Car(brand: "Audi", color: "red")
let myFord = Car(brand: "Ford", color: "green", wheels: 3)
var myCars = [Car]()
myCars.append(myAudi)
myCars.append(myFord)
for aCar in myCars {
println("My (aCar.brand!) is (aCar.color!) and has (aCar.wheels!) wheels.")
}
结果:我的奥迪是红色的,有四个轮子。
我的福特是绿色的,有三个轮子。
你可以创建一个数组,每个元素作为一个数组。例如
var tempArr : [[String]] = [["Ford","Audi"],["Red","Green"],["4"],["NO"]]
根据你的问题,我猜你需要的是字典而不是数组
var tempDict : [String : [String]] = ["Car" : ["Ford","Audi"],.....]