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

时间:2016-09-16 21:25:22

标签: ios swift nsmutablearray nsmutabledictionary

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

1 个答案:

答案 0 :(得分: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)
}

我不太了解Swift,所以我的语法错了。