swift dictionary get value for key code example
Example 1: how to get key for dictionary in swift
var array_has_dictionary = [
[
"name" : "xxxx",
"age" : "xxxx",
"last_name":"xxx"
],
[
"name" : "yyy",
"age" : "yyy",
"last_name":"yyy"
],
]
cell.textLabel?.text = Array(array_has_dictionary[1])[1].key
Example 2: swift retrieve value from dictionary
var companies = ["AAPL" : "Apple Inc", "GOOG" : "Google Inc", "AMZN" : "Amazon.com, Inc", "FB" : "Facebook Inc"]
for (key, value) in companies {
print("\(key) -> \(value)")
}
for value in Array(companies.values) {
print("\(value)")
}
print(companies["AAPL"])
Example 3: create dictionary swift
var someDict:[String:Int] = ["One":1, "Two":2, "Three":3]
var someVar = someDict["One"]
print("The value of key = One is \(someVar)")
Example 4: swift dictionary get key from valye
let dict: [Int: String] = [1: "one", 2: "two", 4: "four"]
if let key = dict.someKey(forValue: "two") {
print(key)
}