2016-09-16 102 views
0

我是新來的Swift和實驗。嘗試創建一個簡單的字典值數組。這很簡單,但我簡單地爲字典對象複製了一小段代碼,更改了每個部分中的值,然後將這四部分中的每一個添加到數組中。爲什麼在將字典添加到數組時添加

當我打印數組時,我發現它包含最後一個字典對象的四個版本。怎麼可能?我會期望該數組有四個字典對象,即添加每個對象之一。

func countDown() { 
    let dict: NSMutableDictionary = [:] 
    let enduroArrayFile = NSMutableArray() 

    dict.setObject(1, forKey: "SectionDistance") 
    dict.setObject("S", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    dict.setObject(1.2, forKey: "SectionDistance") 
    dict.setObject("S", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    dict.setObject(2, forKey: "SectionDistance") 
    dict.setObject("R", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    dict.setObject(2.1, forKey: "SectionDistance") 
    dict.setObject("S", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    print (enduroArrayFile) 
} 

輸出是

(
    { 
    ArrivalTime = 0; 
    AverageSpeed = 0; 
    Direction = S; 
    SectionDistance = "2.1"; 
}, 
    { 
    ArrivalTime = 0; 
    AverageSpeed = 0; 
    Direction = S; 
    SectionDistance = "2.1"; 
}, 
    { 
    ArrivalTime = 0; 
    AverageSpeed = 0; 
    Direction = S; 
    SectionDistance = "2.1"; 
}, 
    { 
    ArrivalTime = 0; 
    AverageSpeed = 0; 
    Direction = S; 
    SectionDistance = "2.1"; 
    } 
) 
+3

因爲NSDictionary是*引用類型*。您可以使用4個引用來創建一個具有相同一對一字典的數組。 –

+0

請考慮使用Swift值類型:Dictionary,Array,... –

回答

4

您錯誤地一遍又一遍地重複使用dict。您需要每次創建一個新實例。

func countDown() { 
    let enduroArrayFile = NSMutableArray() 

    var dict: NSMutableDictionary = [:] 
    dict.setObject(1, forKey: "SectionDistance") 
    dict.setObject("S", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    dict = [:] 
    dict.setObject(1.2, forKey: "SectionDistance") 
    dict.setObject("S", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    dict = [:] 
    dict.setObject(2, forKey: "SectionDistance") 
    dict.setObject("R", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    dict = [:] 
    dict.setObject(2.1, forKey: "SectionDistance") 
    dict.setObject("S", forKey: "Direction") 
    dict.setObject(0, forKey: "ArrivalTime") 
    dict.setObject(0, forKey: "AverageSpeed") 
    //saving dictionary to array 
    enduroArrayFile.addObject(dict) 

    print (enduroArrayFile) 
} 

我不知道斯威夫特很好,所以我可能得到了錯誤的語法。

+0

謝謝。你釘了它。我的想法與此類似,但我預計會看到* first *字典值重複四次,而不是最後一組值。看到最後一集四次讓我完全困惑。任何想法爲什麼? – Jeremy

+0

當然。字典的值一遍又一遍地被替換。最後一個勝利。 – rmaddy