Swift arrays are used to store sequential lists of values of the same type. Swift has to strictly check that it does not allow the wrong type to be stored incorrectly in the array.
If the assignment creates an array to a variable, it is always mutable, which means that you can change it by adding elements, delete or change its items, but if you assign an array constant to the array, the array is immutable, and its size and content cannot be changed.
Create an array
You can use the following initialization program syntax to create an empty array of some kind:
Copy Code code as follows:
var somearray = [SomeType] ()
The following is the syntax for creating a given size with an array of initial values:
Copy Code code as follows:
var somearray = [SomeType] (count:numbeofelements, Repeatedvalue:initialvalue)
The following is an example to create an empty array of type int with 3 elements with an initial value of zero:
Copy Code code as follows:
var someints = [Int] (Count:3, repeatedvalue:0)
The following is an example of an array that creates three elements and a three-valued one:
Copy Code code as follows:
var someints:[int] = [10, 20, 30]
Accessing arrays
You can use the following banner to retrieve the corresponding value from the array, passing the value of the index in parentheses behind the array name, as follows:
Copy Code code as follows:
var somevar = Somearray[index]
Here, the index starts at 0, which means you can use the index 0来 to access the first element, and the second element can be accessed by using index 1, and others are similar. Let's take a look at the following examples of creating, initializing, and accessing arrays:
Copy Code code as follows:
Import Cocoa
var someints = [Int] (Count:3, repeatedvalue:10)
var somevar = someints[0]
println ("Value of the A-is \ \ (somevar)")
println ("Value of second element is \ (Someints[1])")
println ("Value of third element is \ (Someints[2])")
When the above code is compiled and executed, it produces the following results:
Value of the A-is-second element is ten value of
third element is 10
Modifying an array
You can use the Append () method or the addition assignment operator (+ =) to add a new item to the end of the array, where you first create an empty array, and then add a new element to the array, as follows:
Copy Code code as follows:
Import Cocoa
var someints = [Int] ()
Someints.append (20)
Someints.append (30)
Someints = [40]
var somevar = someints[0]
println ("Value of the A-is \ \ (somevar)")
println ("Value of second element is \ (Someints[1])")
println ("Value of third element is \ (Someints[2])")
When the above code is compiled and executed, it produces the following results:
The value of the A-is-second element is value of the
third element is 40
You can modify an existing element of an array by assigning a new value at the given index, as in the following example:
Copy Code code as follows:
Import Cocoa
var someints = [Int] ()
Someints.append (20)
Someints.append (30)
Someints = [40]
Modify last Element
SOMEINTS[2] = 50
var somevar = someints[0]
println ("Value of the A-is \ \ (somevar)")
println ("Value of second element is \ (Someints[1])")
println ("Value of third element is \ (Someints[2])")
When the above code is compiled and executed, it produces the following results:
The value of the A-is-second element is value of the
third element is 50
Iteration/Traversal Array
You can use the for-in loop to iterate through the series, and the following example is the entire set value of the array, as shown in the following illustration:
Copy Code code as follows:
Import Cocoa
var somestrs = [String] ()
Somestrs.append ("Apple")
Somestrs.append ("Amazon")
Somestrs + + ["Google"]
For item in Somestrs {
println (item)
}
When the above code is compiled and executed, it produces the following results:
You can also use the enumerate () function, as shown in the following example, which returns the index and the corresponding value:
Copy Code code as follows:
Import Cocoa
var somestrs = [String] ()
Somestrs.append ("Apple")
Somestrs.append ("Amazon")
Somestrs + + ["Google"]
For (index, item) in enumerate (somestrs) {
println ("Value at index = \ [index] is \" (item))
}
When the above code is compiled and executed, it produces the following results:
Value at index = 0 was Apple
value at index = 1 is Amazon
value at index = 2 is Google
Add two arrays
Use the addition operator (+) to add an array of the same type, which will result in the new array being an array of two array values added together, as follows:
Copy Code code as follows:
Import Cocoa
var Intsa = [Int] (Count:2, Repeatedvalue:2)
var intsb = [Int] (Count:3, repeatedvalue:1)
var intsc = Intsa + INTSB
For item in INTSC {
println (item)
}
When the above code is compiled and executed, it produces the following results:
Count Property
You can use the read-only calculation (count) array property to find the number of elements in the following list:
Copy Code code as follows:
Import Cocoa
var Intsa = [Int] (Count:2, Repeatedvalue:2)
var intsb = [Int] (Count:3, repeatedvalue:1)
var intsc = Intsa + INTSB
println ("total items in Intsa = \ (intsa.count)")
println ("total items in INTSB = \ (intsb.count)")
println ("total items in INTSC = \ (intsc.count)")
When the above code is compiled and executed, it produces the following results:
Total items in Intsa = 2 Total items in
INTSB = 3 Total
items in INTSC = 5
Empty property
Use the null attribute of a read-only group (IsEmpty) to find out whether an array is empty, as shown in the following illustration:
Copy Code code as follows:
Import Cocoa
var Intsa = [Int] (Count:2, Repeatedvalue:2)
var intsb = [Int] (Count:3, repeatedvalue:1)
var INTSC = [Int] ()
println ("Intsa.isempty = \ (intsa.isempty)")
println ("Intsb.isempty = \ (intsb.isempty)")
println ("Intsc.isempty = \ (intsc.isempty)")
When the above code is compiled and executed, it produces the following results:
Intsa.isempty = False
Intsb.isempty = False
Intsc.isempty = True