为什么将字典添加到数组时会重复

Why duplicates when adding dictionary to array

我是 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";
    }
)

您错误地一遍又一遍地重复使用 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)
}

我不太了解 Swift 所以我可能语法有误。