The swift language provides both array and dictionary collection types, which are used to store the same data type sequentially, and the dictionary is a key-value pair that stores data of the same type. In Swift, data and collection stores must have a clear data type that can only access the specified data type.
Array
Arrays are ordered collections that store data of the same type, and the same value can appear multiple times in an array.
Declaring an array
var arraylist:stting[] = ['zampo','is','best ']
The ArrayList variable is declared as an array of type string, marked with shring[], so that only data of the string type can be stored.
Since Swift will automatically determine the type, we do not need to define the array type clearly, ArrayList can write:
var arrayList = ['zampo','is','best' ]
Accessing and modifying arrays
Gets the number of data items in the array:
println ("The array list contains \ (arrayList.count) items")
// Output: The array list contains 3 items
Determine if the array is empty:
if arrayList.isEmpty{
println("The array list is empty")
}else{
println("The array list is not empty")
}
To add new data to the array after using append:
arrayList.append ("hey");
// The fourth piece of data!
arrayList + = "eady"
// use (+ =) to add data after the array
arrayList + = ["not", "the", "best"]
Use subscript to get the data:
var FirstItem = arraylist[0]
Use subscript to modify the data:
arraylist[0"Swift"
arraylist[3...5["per", "happy"]//Replace subscript is 3 and 5, delete between 3 and 5
Call the array's insert (Atindex:) method to add a data item before a specific index value:
Arraylist.insert ("test", Atindex:2)// at this point, test becomes the third item of the array
To move the value of an item in an array:
var test = arrayList.removeAtIndex (2)
// At this time, the index of 2 is removed, and the method returns the value of the removed index. At this time, the data will automatically fill the index.
Move the first element of the divisor group, the last element:
arrayList.removeAtfirst ()
// delete the first item
arrayList.removeAtLast ()
// Remove the end so that we don't need to get the number of arrays by counting
Swift Collection types