Summary of some new changes in the collection class data structure of Swift 3.0

Source: Internet
Author: User
Tags set set
I. Changes to array arrays

An example of the API modified in array arrays is as follows:

Create an array of the same elements//create an array of 10 elements of type string, and each element is the string "Hello"//swift2.2//var array3 = [string] (Count:10, Repeatedvalue: " Hello ")//swift3.0var array3 = [String] (repeating:" Hello ", count:10)//Create an array with 10 elements of type int, and each element is 1//swift2.2//var Array4 = Array (count:10, repeatedvalue:1)//swift3.0var array4 = Array (repeating:1, count:10) var array = [1,2,3,4,5,6,7 , 8,9]//appends a set of elements to the array//swift2.2//array.appendcontentsof ([11,12,13])//swift3.0array.append (contentsof: [11,12,13])// Inserts an element into a position in the array//swift2.2//array.insert (0, atindex:0)//swift3.0array.insert (0, at:0)//Inserts a set of elements into a position in the array//swift2.2/ /array.insertcontentsof ([ -2,-1], at:0)//swift3.0array.insert (contentsof: [ -2,-1], at:0)//Remove an element from a position in an array//swift2.2// Array.removeatindex (1)//swift3.0array.remove (at:1)//Remove an element within a range//swift2.2//array.removerange (0...2)// Swift3.0array.removesubrange (0...2)//Modify an element within a range//swift2.2//array.replacerange (0...2, with: [0,1])// Swift3.0array.replacesubrange (0...2, with: [0,1])//Array enumeration traversal will output (0,0) ((2,2) (3,3) (+)//swift3.0 will enumerate properties enUmerate modified to enumerated () method for item in arraylet.enumerated () {print (item)}var arraysort = [1,3,5,6,7]//Gets the maximum value in the array// Swift2.2//arraysort.maxelement ()//swift3.0arraysort.max ()//Gets the minimum value in the array//swift2.2//arraysort.minelement ()// Swift3.0arraysort.min ()//from large to small sort//swift2.2//arraysort = Arraysort.sort (>)//swift3.0arraysort = arraySort.sorted ( Isorderedbefore: >)//From small to large sort//swift2.2//arraysort = Arraysort.sort (<)//swift3.0arraysort = arraysort.sorted ( Isorderedbefore: <)

ii. changes in set set

Examples of modifications in the Set collection are as follows:

Create set set var Set1:set
 
  = [1,2,3,4]//to move the subscript//Get a subscript after an element//swlft2.2//set1[set1.startindex.successor ()]//swift3.0set1[set1.index (after: Set1.startindex)]//gets a few elements after a subscript//swift2.2//set1[set1.startindex.advancedby (3)]//swift3.0set1[set1.index ( Set1.startindex, Offsetby:3)]//gets the maximum value in the collection//swift2.2//set1.maxelement ()//swift3.0set1.max ()//Gets the minimum value in the collection//swift2.2/ /set1.minelement ()//swift3.0set1.min ()//remove elements from a position in the collection//swift2.2//set1.removeatindex (Set1.indexof (3)!) Swift3.0set1.remove (At:set1.index (of:3)!) var set3:set
  
   
    = [1,2,3,4]var set4:set 
    
     = [1,2,5,6]//return intersection {1,2}//swift2.2//var Setinter = Set3.intersect (SET4)//SWIFT3.0VA R setinter = set3.intersection (SET4)//return complement of intersection {3,4,5,6}//swift2.2//var Setex = Set3.exclusiveor (SET4)//swift3.0var Setex = Set3.symmetricdifference (set4) var set5:set = [1,2]var set6:set = [2,3]var set7:set = [1,2,3]var Set8:Set = [+/-] Determines whether a subset of a set SET5 is a subset of Set7 returns Ture//swift2.2//set5.issubsetof (SET7)//swift3.0set5.issubset (OF:SET7)// Determines whether a superset of a set SET7 is a superset of Set5 returns Ture//swift2.2//set7.issupersetof (SET5)//swift3.0set7.issuperset (OF:SET5)// Determines whether a true subset of a set SET5 is a true subset of Set7 returns Ture//swift2.2//set5.isstrictsubsetof (SET7)//swift3.0set5.isstrictsubset (OF:SET7)/ /Determine if a true superset of a set SET7 is not a true superset of Set8 returns False//swift2.2//set7.isstrictsupersetof (SET8)//swift3.0set7.isstrictsuperset (of : set8)
     

   
  
 

iii. changes in the dictionary dictionary

Examples of modifications in the dictionary dictionary are as follows:

Remove a key-value pair//swift2.2//dic1.removevalueforkey (1)//swift3.0dic1.removevalue (forkey:1) by key

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