// Playground-noun: a place where people can play
import UIKit
// ------------------------------------------------ ------------------------------
// 1. Define a fixed-format dictionary
// All "key value" types of dict1 are consistent (both strings)
var dict1 = ["name": "mary", "age": "18"]
// ------------------------------------------------ ------------------------------
// 2. In the definition dictionary, you can directly specify the type of "key name" and "key value" in the dictionary
// Once the dictionary's key name and key value type are specified, it can no longer be modified
var ages: Dictionary <String, Int> = ["jack": 20, "rose": 19]
// ------------------------------------------------ ------------------------------
// 3. Dictionary operations
// Can be directly operated for fixed-format dictionaries
// 1> Get the dictionary key value by key value. Since the dictionary format is fixed, there is no need to specify the variable type
// *** Note that the operation results specified by the "Specify / Cancel" value type are different
var name = dict1 ["name"] // as String
name = "mike" + "\ (name)"
// *** Because you may get a nil when getting a value from a dictionary, you need to judge if you need to process the data later
if var age = dict1 ["age"] ?. toInt () {
age + = 20
}
// 2> Two ways to modify dictionary keys
dict1 ["name"] = "rose"
dict1
dict1.updateValue ("18", forKey: "age")
dict1
// 3> delete a certain key value
dict1.removeValueForKey ("age")
dict1
// 4> add new key-value pairs
dict1 ["height"] = "1.65"
dict1