// Playground-noun: a place where people can play
import UIKit
// ------------------------------------------------ ------------------------------
// 1. Define a fixed-format dictionary
// all dict1 "key value" types 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 manipulated for fixed-format dictionaries
// 1> Get the dictionary key value with the key value. Since the dictionary format is fixed, there is no need to specify the variable type
// *** Note that the operation result specified by "Specify / Cancel" value type is different
var name = dict1 ["name"] // as String
name = "mike" + "\ (name)"
// *** Because you may get a nil when fetching a value from a dictionary, if you need to process the data later, you need to judge
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
Swift basic usage-definition of dictionary nsdictionary, modifier key value, delete/Add key value