I'm new to Swift and experimenting. Trying to create a simple array of dictionary values. This is crude, but I simply replicated a short piece of code four times for the dictionary objects, changed values in each section and then added each of the four to the array.
When I print the array, I am finding it contain four versions of the last dictionary object. How can that be? I would have expected the array for have four dictionary objects i.e one of each of the objects added.
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)
}
The output is
(
{
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";
}
)