Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
How to loop over a dictionary in Swift 5?

In Swift 4 I could the code below worked but in Swift 5 I get the following error: Type 'Dictionary<String, String>.Values.Iterator' does not conform to protocol 'Sequence'

guard let userIds = users.values.makeIterator() else { return }

for userId in userIds {
    // User setup
}

What is the right way in Swift 5 now?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

let dictionary: [String: Int] = ["a": 1, "b": 2]

for (key, value) in dictionary {
    print(key, value)
}
over 4 years ago · Santiago Trujillo Report

0

You may try iterator like this:

 let users = ["a":11, "b":12]
 var userIds = users.values.makeIterator()
 while let next = userIds.next() {
    print(next) // 11 \n 12
 }
over 4 years ago · Santiago Trujillo Report

0

Also:

let x = [
"kitty": 7,
"bob": 2,
"orange": 44
]
x.forEach { t in
    print("key = \(t.key); value = \(t.value)")
}

This has apparently been available since Swift 3. Link to standard library docs on Dictionary type.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!