Avatar ·

How to get an array of keys from a dictionary in Swift?

Hello everyone! I'm new to Swift and encountered a problem when working with dictionaries. I have a dictionary that stores user names and their ages:\n\n```swift\nlet users: [String: Int] = ["Alice": 25, "Bob": 30, "Charlie": 22]\n```\n\nI need to get an array containing only the keys (user names), i.e., `["Alice", "Bob", "Charlie"]`. I tried using the `.keys` property, but it returns not an array, but some collection `Dictionary.Keys`. When I try to use this result as a regular array, Xcode gives a type error.\n\nPlease tell me how to properly convert dictionary keys into a regular `[String]` array? Preferably with code examples. Thanks in advance for your help! <<>>

Using Array(users.keys)

The simplest and most ra