-4

I have following dictionary

var data : [String: String] = ["DEV" : "DEV", "QUA" : "QUA" , "SIT" : "SIT", "UAT" : "UAT", "PROD" : "PROD"]

I Iterate it using for loop and get the following values

SIT
PROD
DEV
UAT
QUA

ie consecutive values

i have the following code to iterate it

for (key, value ) in data.enumerate()
{
    print(value)
}

I want to get these values in same format of declaration.

ADK
  • 139
  • 10

1 Answers1

0

Swift Dictionary type is unordered type. On every for in loop you can receive different ordered result (in theory). You should choose other data structure if you wish to keep order, Array<(String, String)> for example:

var data: Array<(String, String)> = [("DEV", "DEV"), ("QUA", "QUA"), ("SIT", "SIT"), ("UAT", "UAT"), ("PROD", "PROD")]

for element in data {
    print(element.1)
}
//DEV
//QUA
//SIT
//UAT
//PROD
Yury
  • 6,044
  • 3
  • 19
  • 41