swift--Dictionary (dictionary)

Source: Internet
Author: User

1. Creation and initialization of dictionaries

2. Create an empty dictionary

3. Adding, modifying, and deleting data from a dictionary

4. Get value based on key

5. Convert value to a specific type

6. Enumerate the keys and value in the dictionary

7. Convert key and value to array

1 varemployee1:dictionary<string,string> = ["name":"Bill"," Company":"Microsoft"]2println (EMPLOYEE1)//[Company:microsoft,name:bill] sorted in alphabetical order. 3 4 varEmployee2 = ["name":"John"," Company":"Oracle"," Age": +]//without specifying a type, you can set it to another type, such as an integer5println (EMPLOYEE2);

1 varDict1 = ["Product_Name":"Ice Cream"," Price":5.5]2println (Dict1)//Price = "5.5; "Product_Name" = "ice cream", converts 5.5 into a string. If an integer is used, the output remains an integer. 3 4 varDict = Dictionary<int, string> ()//Create an empty dictionary5dict[ -]="Hello World"             //No 20 is filled, and 20 is modified. 6println (Dict)//[20:hello World]7 8Dict = [:]//empty array, key type is Int,value type is string9 Tendict[ +]="ABCD" Onedict[ -]="XYZ" Aprintln (Dict)//[20:XYZ,21:ABCD] -  - ifLet OldValue = Dict.updatevalue ("airplane ", forkey:21)//If there are 21 in the dictionary, the previous value is returned, and nil is returned if there is no 21. In the SWIFT statement, there is no parentheses after the IF.  the { -println"the old value is \ (oldValue)")//the old value is ABCD - } -println (Dict)//[20:xyz,21: Aircraft] +println (Dict.count)//2 -  +Let Age1:int = employee2[" Age"] asInt//Age is unexpected . Aprintln (AGE1);// + at  - ifLet Age2:int? = (employee2[" Age"] as? INT)//If this age does not exist, then nil will be returned, but int cannot save nil, so the post-add? //here output 32, if changed to employee2["Age1"], then return does not exist this key. - { - println (age2) - } - Else in { -println"This key does not exist in the dictionary")        to}
1dict[ -] = Nil//Delete a key in the dictionary directly, and the corresponding value is also deleted. Key and value always appear in pairs. 2println (Dict)//[21: Airplane] 20 has been deleted3 4Let OldValue = Dict.removevalueforkey ( +)//returns 21 for the corresponding value5println (OldValue)//Aircraft6 7 //enumerating keys and value in a dictionary8  for(Key, value)inchemployee19 {Tenprintln"\ (key): \ (value)");  One } A  -  forKeyinchEmployee1.keys - { the println (Key) - } -  -  forKeyinchEmployee2.keys//error, Employee2 is implicitly declared dict, so the keys will not be found + { - } + //Convert A  forKeyinch(Employee2 asDictionary). Keys at { - println (Key) - } -  -  forValueinchemployee1.values - { in println (value) - } todict[ A] ="ABC" +Let keys =Array (Dict.keys) -println (keys)//[] the  *Let values =Array (dict.values) $println (values)//[ABC]

swift--Dictionary (dictionary)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.