1 Import Foundation2 //the Order of the dictionary elements is unordered3 //1. The dictionary element is a key-value pair (Key:value)4 //key must be a hash string\int\bool5 vardic1=["name":"LSQ"]6println (dic1["name"])7 //the complete definition method8 vardic2:dictionary<string,any>=["a":1,"b":"b"]9 println (DIC2)Ten One //2. Operation of the dictionary A vardic3=["Apple":"Apple","Bag":"Package"] -println (dic3["Apple"])//Enquiry -dic3["Apple"]="Chestnut"//Modify theprintln (dic3["Apple"]) -Dic3.updatevalue ("Apple", Forkey:"Apple")//Save, return key value to the value of the optional type update failed return value is nil -println (dic3["Apple"]) -println (dic3["Apples"])//access to a nonexistent key gets an empty nil + -Dic3.updatevalue ("Apple AAA", Forkey:"Apples")//modified to return a key value to an optional type of value update failed the return value is nil if the element is not added + println (DIC3) A atdic3["LSQ"]="Liu Shiqun" //Add an element - println (DIC3) - -Dic3.removevalueforkey ("LSQ")//deletes the specified key to return the removed value if key does not return nil in the dictionary - println (DIC3) - //Dic3.removeall (keepcapacity:true)//Delete all as an array parameter indicates whether the dictionary capacity is preserved in - //the traversal of a dictionary to for(Key,value)inchDIC3 { +println"key:\ (Key) value:\ (value)") - } the * //Keys Values $ println (Array (Dic3.keys))Panax Notoginseng println (dic3.values) - the forKeyinchdic3.keys{ + println (Key) A } the forValueinchdic3.values{ + println (value) -}
Beginner Swift Notebook dictionary, Array (iv)