0

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";
    }
)
2
  • 3
    Because NSDictionary is a reference type. You create an array with 4 reference to the same one-and-only dictionary. Commented Sep 16, 2016 at 21:27
  • Consider to use Swift value types instead: Dictionary, Array, ... Commented Sep 16, 2016 at 21:33

1 Answer 1

4

You are mistakenly reusing dict over and over. You need to create a new instance each time.

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

I don't know Swift well so I may have gotten the syntax wrong.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. You nailed it. I was thinking along similar lines but I would have expected to see the first dictionary values repeated four times, not the last set of values. Seeing the last set four times has me totally confused. Any idea why?
Sure. The dictionary's values are being replaced over and over. The last one wins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.