Swift offers 3 main collection types, array,set,dictionary. This section describes the array.
An array is a collection of the same types that are stored in an orderly manner, and the same values can appear multiple times in different locations.
Attention:
The array type of swift bridges the Nsarray class of foundation
Array type simple syntax
Swift array type complete writing array<element>,element is the legal type of array that allows you to store values, and you can simply write [Element]. Although the two forms are functionally identical, we recommend a shorter one and use this form to use arrays in this article.
First, create an array
1. Create an empty array
It is important to note that the type of the someints variable is inferred as an int at initialization, or if the context already provides type information, such as a function argument or a constant or variable of a defined type, we can also write [] directly without adding an int.
Someints.append (3)// someints now contains 1 value of type Intsomeints = [] // someints is a empty array, but still of type [Int]
3. Create an array with default values
The array type in Swift also provides a construction method that can create a specific size and that all data is set to the same default value. We can pass in the array constructor the item number (count) and the appropriate type's initial value (Repeatedvalue) that are ready to be added to the array:
var 3 0.0 )// threedoubles is of type [Double], and equals [0.0, 0.0, 0.0]
4.2 Arrays merged into an array
by + to implement 2 number combinations for a new array
var 3 2.5 )// anotherthreedoubles is of type [Double], and equals [2.5, 2.5, 2.5]var sixdoubles = Threedoubles + anotherthreedoubles// sixdoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
5.[value 1, value 2, value 3]
var shoppinglist: [String] = ["Eggs""Milk"]
This shows that shoppinglist is an array variable that stores string types and can only store values of type string. It can also be a simple writing
var shoppinglist = ["Eggs" "Milk"]
Ii. access and modification of arrays
We can access and modify the array by using the methods and properties of the array, or the next banner method. Use the read-only property of the array count to get the count of the array.
Print ("Theshopping list contains \ (shoppinglist.count) items. " )// Prints "The shopping list contains 2 items.
Use IsEmpty to determine if the array is empty.
1. Getting array data
Subscript method
var firstitem = shoppinglist[0]// FirstItem is equal to "Eggs
2. Add Data
Use the Append (_:) method to add a new piece of data at the end of the array.
Shoppinglist.append ("flour")
You can also add several new data to the array using + =
Shoppinglist + = ["baking Powder"]// shoppinglist now contains 4 items shoppinglist + = ["chocolate Spread" "Cheese" " Butter " ]// shoppinglist now contains 7 items
Insert new data at the specified location using insert (_:atindex:)
Shoppinglist.insert ("Maple syrup"0)// shoppinglist now Contains 7 items// "Maple syrup" is now the first item in the list
3. Modifying an array
Subscript method to modify one of the values
shoppinglist[0"Six Eggs"
You can also modify values within a specified range
shoppinglist[4... 6] = ["Bananas""Apples"]// shoppinglist now contains 6 items
We cannot add new data at the end of the array by subscript, because the index is not legal after it exceeds the length of the array, and a run-time error can occur
Removeatindex (_:) to delete data from the specified position in the array
Let Maplesyrup = Shoppinglist.removeatindex (0)// the item is at Index 0 have just been Removed// shoppinglist now contains 6 items, and no Maple syrup// the Maplesyrup cons Tant is now equal to the removed "Maple syrup" string "
If you delete the last piece of data in the array, you should use Removelast () instead of Removeatindex () because it does not have to query the length of the array.
Iv. Traversal of arrays
You can use the for-in ' loop to iterate through the array.
for inch shoppinglist { print (item)}
If we need both values and index values for each data item, you can use the global enumerate function for array traversal. Enumerate returns a set of key-value pairs consisting of each data item index value and data value. We can break this key value pair into a temporary constant or variable to traverse:
for inch shoppinglist.enumerate () { print ("Item \ (index + 1): \ (value)")} // Item 1:six Eggs // Item 2:milk // Item 3:flour // Item 4:baking Powder // Item 5:bananas "
Swift Learning Swift Programming Tour---Array of collection types (vi)