Common methods of Swift dictionaries

Source: Internet
Author: User

var dict1 = [1: "A", 2: "B", 3: "C", 4: "D", 5: "E"];
var test1 = ["Key1": "Hello", "Key2": "Swift", "Key3": "Learning", "Key4": "Dictionary", "Key5": "Value"]
To take a value according to the key
Print (test1["Key1"]);
Number of dictionary elements
Print (Test1.count);
Add a dictionary element
DICT1[6] = "F";
test1["key"] = "test";
Delete an element in a dictionary
Test1.removevalue (forkey: "Key1");
Modifying a dictionary element
test1["key"] = "Li ping";
Traverse
for (Key,value) in test1{
Print ("key\ (Key), value,\ (value)");
}
For TestKey in test1.keys{
Print (TestKey);
}
For TestValue in test1.values{
Print (TestValue);
}
Convert a key or value in a dictionary to an array
Let Textkeyarr = Array (Test1.keys);
Print (Textkeyarr);
Let Textvaluearr = Array (test1.values);
Print (Textvaluearr);

Common methods of Swift dictionaries

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.