Collection type learning notes built into Swift _swift

Source: Internet
Author: User

First, the introduction

Swift provides 3 types of collections, array data types, set collection types, and dictionary dictionary types. Array is used to hold a set of ordered data, the data angle is incremented from 0; set is used to hold a set of unordered data, data cannot be duplicated, dictionary is also used to hold a set of unordered data, but it is stored according to the key value pair, the key value must be unique. Here is a picture from an official document that shows the characteristics of 3 types of collections:

Two, array type

Array is often also called an array, and Swift is a type-safe language in which the array type must also determine the type of its elements, and there are two ways to declare an array type, as in the following example:

Declare an array as an array of type-value sets of Int
var array1:[int]
var array2:array<int>
//Create an empty array
array1 = []
array2 = Array ()

If an array object is also received through the Var variable, it is a mutable array, and the element can be appended by the Append method, as shown in the following example:

Append element array1.append to array
(3)

When you create an array, you can also initialize arrays, as shown in the following example:

Create an array [0,0,0]
var array3 = [Double] (Count:3, repeatedvalue:0)
//Create an array [2.5,2.5,2.5]
var array4 = Array ( Count:3, repeatedvalue:2.5)
//array can be appended directly with the + number [0,0,0,2.5,2.5,2.5]
var array5 = Array3+array4

Swift provides many ways to access and modify arrays, with sample code as follows:

Gets the number of elements in the array
array5.count
//To determine whether the array is empty array5.isempty//to
access the elements in the array by subscript
array5[1]
// Modify the array element
array5[1]=2
//Modify a set of data in the data
array5[0...3] = [1,1,1,1]
//Insert a data
to a location in the array Array5.insert (3, atindex:1)
//Shift the element of a corner of an array
array5.removeatindex (1)
//Shift the last element
of the divisor group Array5.removelast ()
///move the first element of
the divisor group Array5.removefirst ()
///traverse the entire array for
item in Array5 {
  print ( Item)
}
///Traversal Array enumeration for
(Index,item) in Array5.enumerate () {
  print (Index,item)
}

Three, set type

Set type collections do not pay attention to the order of elements, but they guarantee the uniqueness of the elements. As with the array type, it is also necessary to determine the type of the inner element when declaring the set type, as shown in the following example:

var set1:set<character> = ["A", "B", "C", "D"]

The following example code demonstrates an action on a collection:

var set1:set<character> = ["A", "B", "C", "D"]
var set2:set<character> = ["E", "F", "G"]
///Insert element into the collection
Set1.insert ("z")
//Get the number of elements in the collection
Set1.count
//Determine if the collection is empty
set1.isempty
//Remove
an element from the collection Set1.remove ("a")
//Remove all elements in the collection
Set1.removeall ()
//To determine if an element is contained in the collection
set2.contains ("E")
// Traverse collection
for item in Set2 {
  print (item)
}
//To sort the small to large to traverse the for
item in Set2.sort () {
  print (item )
}

Set also supports the mathematical operation of some sets, such as intersections, sets, complements, and so on, and the following diagram illustrates some of the characteristics of set operations:

    • The Intersect () method returns the intersection of two sets.
    • The Exclusiveor () method returns a complement of two set intersections.
    • The Union () method is used to return the set of two sets.
    • The subtract () method returns the complement set for the second collection.

The sample code is as follows:

var set3:set<int> = [1,2,3,4]
var set4:set<int> = [1,2,5,6]
//return intersection {1,2}
var setinter = Set3.intersect (SET4)
//Returns the complement of the intersection {3,4,5,6}
var Setex = Set3.exclusiveor (SET4)
//Return and set {1,2,3,4,5,6}
var Setuni = set3.union (SET4)
//Returns the complement of the second collection {3,4}
var setsub = set3.subtract (SET4)

Use comparison operator = = To compare two set sets for equality, and when all elements in two set sets are equal, the two sets are equal. The following code shows the operations associated with a subset:

var set5:set = [1,2]
var set6:set = [2,3] var
set7:set = [1,2,3]
var set8:set = [1,2,3]
//Determine if it is a subset of a set Set5 is a subset of Set7 return ture
set5.issubsetof (SET7)
//To determine whether the superset of a set SET7 is a superset of Set5 returns ture Set7.issupersetof
( SET5)
//To determine whether a true subset of a set SET5 is a true subset of Set7 return ture
set5.isstrictsubsetof (SET7)
//To determine whether a true superset of a set Set7 is not a set8 true superset returns false
Set7.isstrictsupersetof (SET8)

Iv. type of Dictionary

Dictionary in Swift must specify the type of the key and the type of the value when declared, as shown in the following example:

var dic:dictionary<int,string>
var dic2:[int:string] = [1: "One", 2: "Two"]

To access and manipulate the dictionary method, the code example is as follows:

var dic2:[int:string] = [1: "One", 2: "Two", 3: "Three", 4: "Four"]
//Get Dictionary key-value pairs number
Dic2.count
//Judge whether the dictionary is empty
Dic2.isempty//
through key to get value
dic2[1]
//through key Modify value
dic2[1] = "a"/
/Add key value
dic2[0] = "Zero"
// The Updatevalue method updates a key value if the key exists, updates the key value and returns the old key value if the key does not exist, adding the key value returns nil it returns a optional type value that can be processed using if let
dic2.updatevalue ("9", Forkey:1)
Use if let to process updatevalue return value
if let OldValue = Dic2.updatevalue ("One", forkey:1) {
  print ("The old Value is \ \" (Oldval UE)
}
//The data obtained by the key value will also have a value of optional type that can also be used if let-
if let value = dic2[1] {
  print ( Value)
}//
/Remove a key value to
Dic2[9]=nil
Dic2.removevalueforkey (9)
//Dictionary traversal for
(key, Value) in Dic2 {
  print (key,value)
}
//Traverse all keys for
key in Dic2.keys {
  print (key)
}
// Iterate through all values
for value in Dic2.values {
  print (value)
}
//To sort from small to large to traverse for key in
Dic2.keys.sort () { C38/>print (key)
}

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.