ios - Dictionary returns consecutive/shuffled values when iterating in swift -
this question has answer here:
- ordered dictionary in swift 7 answers
- swift - stored values order changed in dictionary 5 answers
i have following dictionary
var data : [string: string] = ["dev" : "dev", "qua" : "qua" , "sit" : "sit", "uat" : "uat", "prod" : "prod"]
i iterate using loop , following values
sit prod dev uat qua
ie consecutive values
i have following code iterate
for (key, value ) in data.enumerate() { print(value) }
i want these values in same format of declaration.
swift dictionary
type unordered type. on every for in
loop can receive different ordered result (in theory). should choose other data structure if wish keep order, array<(string, string)>
example:
var data: array<(string, string)> = [("dev", "dev"), ("qua", "qua"), ("sit", "sit"), ("uat", "uat"), ("prod", "prod")] element in data { print(element.1) } //dev //qua //sit //uat //prod
Comments
Post a Comment