​Quick is really a effective and intuitive development language for macOS, iOS, watchOS and tv...

Page 86

Swift

println("Dictionary key \(key) -

Dictionary value \(value)")

} When the above code is compiled and executed, it produces the following result: Dictionary key 1 -

Dictionary value One

Dictionary key 2 -

Dictionary value Two

Dictionary key 3 -

Dictionary value Three

You can use enumerate() function which returns the index of the item along with its (key, value) pair as shown below in the example: import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

for (key, value) in enumerate(someDict) { println("Dictionary key \(key) -

Dictionary value \(value)")

} When the above code is compiled and executed, it produces the following result: Dictionary key 0 -

Dictionary value (1, One)

Dictionary key 1 -

Dictionary value (2, Two)

Dictionary key 2 -

Dictionary value (3, Three)

Convert to Arrays You can extract a list of key-value pairs from a given dictionary to build separate arrays for both keys and values. Here is an example: import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

let dictKeys = [Int](someDict.keys) let dictValues = [String](someDict.values)

println("Print Dictionary Keys")

74


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.