+1 vote
asked in Swift by (4k points)
Hi All

  How to get Dictionary key as separate array in Swift?

1 Answer

+1 vote
answered by (2.6k points)

Hi Kalpana

if you need to use a dictionary’s keys or values with an API that takes an Array instance, initialize a new array with the keys or values property:   

 var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]


  • let airportCodes = [String](airports.keys)
  • // airportCodes is ["YYZ", "LHR"]
  • let airportNames = [String](airports.values)
  • // airportNames is ["Toronto Pearson", "London Heathrow"]