C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
If: We use the if-statement to test whether keys exist in the nested dictionaries. The string "cat" exists, but "diamond" does not.
IfSwift program that creates array of dictionaries
// This is an array of dictionaries.
var dictionaries = [[String: Int]]()
// Create a dictionary and add it to the array.
var dictionary1: [String: Int] = ["cat": 100]
dictionaries.append(dictionary1)
// Create another dictionary.
var dictionary2: [String: Int] = ["dog": 200]
dictionaries.append(dictionary2)
// Get value from dictionary in array element 0.
if let value = dictionaries[0]["cat"] {
print(value)
}
// Get value from dictionary that does not exist.
if let value = dictionaries[1]["diamond"] {
// This is not reached.
print("Not reached")
}
Output
100