Studying-swift:day02

Source: Internet
Author: User

Study Address: http://www.rm5u.com/or http://www.runoob.com/

  1. 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

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.