2015-06-02 86 views
1

多維數組我想聲明數組應該是這樣的:Delcare並獲得在迅速

myArray 
    ->Car (Array) 
    - Ford 
    - Audi 
    - ... 
    ->Color (Array) 
    - Red 
    - Green 
    - ... 
    ->Wheels (Simple String, eg "4") 
    ->Roof (Simple String, eg, "NO") 

林停留在空間delare和訪問該類型的數組。感謝您的任何幫助!

回答

2

我覺得創建自定義對象(與類或結構)會更適合你,像這樣的例子:

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.") 
} 

結果:

我的奧迪是紅色的,有4個車輪。
我的福特是綠色的,有3個車輪。

注意:看看this,它會有所幫助。

0

你可以創建一個數組作爲數組。例如根據你的問題

var tempArr : [[String]] = [["Ford","Audi"],["Red","Green"],["4"],["NO"]] 

我猜你需要一本字典,而不是一個數組

var tempDict : [String : [String]] = ["Car" : ["Ford","Audi"],.....]