Study Address: http://www.rm5u.com/or http://www.runoob.com/
- Swift dictionary:
• Create a dictionary:
Create an empty dictionary of a specific type:
var somedic = [keytype:valuetype] (): var somedic = [int:string] ()
Create an instance of a dictionary:
var somedic:[int:string] = [1: "one", 2: "one", 3: "three"]
• Access Dictionaries:
Access the array elements according to the index of the Dictionary:
var Somevar = somedic[key]
• Modify the Dictionary:
You can use Updatevalue (forkey:) Add or update the contents of the Dictionary. If key does not exist, the value is added and if present, the value corresponding to the key is Modified. The Updatevalue (_:forkey:) method returns the optional Value.
var oldval = Somedic.updatevalue ("new value of one", forkey:1)
You can also modify the value of the dictionary by specifying the key
somedic[1] = "new value of one"
• Remove Key-value pairs:
You can use the Removevalueforkey () method to remove a dictionary key-value pair. If the key exists, the method returns the removed value if no return nil exists
You can also remove the Key-value (key-value) pair by specifying the value of the key as nil
• Traverse the dictionary:
You can use the for-in loop to traverse a key-value pair in a dictionary:
= [1:"one"2:"both"3:" three"] for in somedict { print ( " dictionary key \ (key)- dictionary value \ (value)")}
You can also use the enumerate () method for dictionary traversal, which returns the index of the dictionary and (key, Value) pairs:
import uikitvar somedict:[int:string] = [1 : one ", 2 : " , 3 : " three " for (key, value) in somedict.enumerate () {print ( " dictionary key \ (key)-dictionary (key, Value) to \ (value) " /span>
· Dictionary conversions to Arrays:
You can extract the Dictionary's key value (key-value) pair and convert it to a separate array:
Let Dictkeys = [Int] (somedic.keys)
Let dictvalues = [String] (somedic.values)
· Count Property
· IsEmpty Property
Studying-swift:day02