Use of dictionary dictionaries in Swift

Source: Internet
Author: User

The Swift dictionary is used to store a collection of unordered data of the same type, and the Swift dictionary enforces the detection of the type of the element, which is an error if the type is different.

Each value of the Swift dictionary is associated with a unique key (key), which is used as the identifier for the value data in the dictionary.

The data items in the dictionary are not in a specific order, unlike the data items in the array. We use dictionaries when we need to access data through identifiers (keys), which is largely the same way that we use dictionaries in the real world to find meanings.

The key for the Swift dictionary does not have a type limit that can be integer or string, but must be unique.

If you create a dictionary and assign a value to a variable, the dictionary you create is modifiable. This means that after you create a dictionary, you can change the items in the dictionary by adding, deleting, and modifying them. If you assign a dictionary to a constant, the dictionary cannot be modified, and the size and contents of the dictionary cannot be modified.


[OBJC]View plain copy//Create a dictionary//create a specific type of empty dictionary in the format: var dict = [Keytype:valuetype] ()//Create an empty dictionary, the type of the key is Int, the value is of a simple type of String Syntax: var dict01 = [int:string] () print (DICT01)//Create an instance of a dictionary: var dict02: [Int:string] = [1: "One", 2: "Tw O ", 3:" Three "] print (dict02) var dict03 = [" Name ":" Devzhang "," Job ":" Iosdev "," Company ":" Vstecs "] Print (d ict03)[OBJC]View plain copy//Access dictionary//We can access the elements of an array based on the index of the dictionary, with the following syntax: var value = Dict[key] Let value01 = dict02[1] Print (VALUE01) Let value02 = dict03["name"] Print (VALUE02)[OBJC]View plain Copy//Add data let Value03 = Dict02.updatevalue ("Four", forkey:4)//or dict02[4] = "Four" print (VALUE03) Print (dict02)[OBJC]View plain Copy//Modify dictionary//Method 1 using 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 format is: the Dict.updatevalue (value, Forkey:key) method returns the optional value. var value

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.