How to get Dictionary key as separate array in Swift

+1 vote
140 views
asked Mar 3, 2015 in Swift by kalpana (3,995 points)
Hi All

  How to get Dictionary key as separate array in Swift?

1 Answer

+1 vote
answered Mar 3, 2015 by codeg (2,600 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"]
...