Collection types in Swift have arrays and dictionaries
The difference between an array and a dictionary is that the array is ordered, the dictionary type is unordered and the dictionary has a key value
1. Definition and updating of arrays
The definition of an array is array<sometype> or [SomeType]
var Yamanote line =["Nippori", "Eagle Valley"]
Yamanote Line. Count can be used to view the number of arrays
The Yamanote Line. IsEmpty can be used to determine if an array is empty
To add elements to an array, you can use the Append method
Yamanote Line. Append ("Ueno")
You can also use a combination assignment method
Yamanote Line +=["Akihabara"]
Yamanote Line +=["Kanda", "Tokyo"]//Add multiple elements at the same time
You can modify the value of an array by using the following method
Yamanote Line [0]= "Shinagawa"
Yamanote Line [1...2]=["Kobe", "Hiroshima"]//modification of the second third element
Inserting a value in an array can use the Insert method
Yamanote Line. Insert ("formerly", atindex:0)//Insert in item No. 0
Yamanote Line. Removeatindex (0)//the No. 0 item in an array returns the item that was removed
Remove Last item
Yamanote Line. Removelast ()//can move the last item in the divisor group
For (ordinal, station name) in enumerate (Yamanote line) {
println ("\ (serial number): \ (station name)")
}
The index in the array can be taken with this method
2. Creation and initialization of arrays
Initialization of an array can use literal
var a=[1,3,2]
You can also initialize an array using the constructor method of the array
Initializing an empty array can be written like this
var a=[int] ()
If each item in the array is the same
var threeint=[int] (count:3,repeatedvalue:0)
3. Definitions and updates of dictionaries
A dictionary can be defined using dictionary<keytype,valuetype> or [Keytype:valuetype]
Define an array using the Literal method
var airports:[string:string]=["Tyo": "Tokyo", "DUB": "Dublin"]
Use the Count property of the dictionary to see the number of elements in the dictionary
airports=[:] You can set the array to null
Use the IsEmpty property to see if the array is empty
You can add a value to the dictionary by using the subscript method
airports["SZX"]= "Shenzhen Bao ' an international airport"
You can also modify the values in the dictionary by subscript
airports["SZX"]= "Shenzhen Bao ' an international Airport 2 floor"
You can also use the method provided by the dictionary type to modify the elements in the dictionary, which returns an optional type
if (Airports.updatevalue ("Shenzhen Baoan International Airport", Forkey: "SZX")) {
println ("Update airport name succeeded!");
}else{
println ("Failed to update airport, no airport code")
}
There is no value for the optional type returned when the. Szx key is not present in the dictionary
Removing elements from a dictionary can use the Remove method
Airports.removevalueforkey ("SZX")//This method returns an optional type
Iterating through the elements in the dictionary can use the for
For (airport code, airport name) in airports{
println ("\ (airport code): \ (airport name)")
}
for (airport code) in airports.keys{
println (airport code)
}
for (airport name) in airports.values{
println (airport name)
}
Let airport code group =[string] (Airports.keys)
Let airport name Group =[string] (airports.values)
4. Creation and initialization of dictionaries
In addition to creating a dictionary in a literal way, you can create an array using the constructor provided by the dictionary type (note that the type of key is hash-capable)
var a=[int:string] ()
a[10]= "Arabic numerals ten"
a=[:]//Empty the dictionary
Swift Collection Types