There are a couple of ways to iterate through a dictionary.
First, you can use the For-In loop, like this:
var myTable = [ "Apple": "iOS", "Google" : "Android", "Microsoft" : "Windows Phone" ]/*from w w w .jav a 2s . c om*/ for platform in myTable { print(platform) }
You can specify the key and value separately:
var myTable = [ "Apple": "iOS", "Google" : "Android", "Microsoft" : "Windows Phone" ] for (company, platform) in myTable { print("\(company) - \(platform)") }
You can use the For-In loop to iterate through the keys inside a dictionary using the keys property:
var myTable = [ "Apple": "iOS", "Google" : "Android", "Microsoft" : "Windows Phone" ] for company in myTable.keys { print("Company - \(company)") }
The following example iterates through the values in a dictionary using the values property:
var myTable = [ "Apple": "iOS", "Google" : "Android", "Microsoft" : "Windows Phone" ] for platform in myTable.values { print("Platform - \(platform)") }
You can also assign the keys or values of a dictionary directly to an array:
let companies = myTable.keys let oses = myTable.values