Swift arrays use ordered lists to store multiple values of the same type. The same value can appear multiple times in different locations in an array.
The Swift array forcibly detects the type of the element, and if the type isdifferent, it will report an error. The Swift array should follow somethinglike
Array<Element>
in this form, of which
Element
is the only data type allowed in this array.
If you create an array and assign a value to a variable, the collection created can be modified. This means that after creating the array, you can change the items in the array by adding, deleting, and modifying. If you assign an array to a constant, the array cannot be changed, and the size andcontents of the array cannot be modified. We can use construction syntax to create an empty array of specific data types: The following is the syntax for creating an initialized size array: The following example creates a type of The following example creates an array of three elements: We can access the elements of the array based on the index of the array. Thesyntax is as follows: The We can learn how to create, initialize, and access arrays through the following examples: The output of the above program execution is as follows: You can use it. The output of the above program execution is as follows: We can also modify the value of the array element through the index: The output of the above program execution is as follows: We can use it. The output of the above program execution is as follows: If we need both the value and the index value of each data item, we can use the The output of the above program execution is as follows: We can use the addition operator (+) to merge two existing arrays of the same type. The data type of the new array is inferred from the data type of the two arrays: The output of the above program execution is as follows: We can use The output of the above program execution is as follows: We can use the read-only property The output of the above program execution is as follows: 9.26.1. Create an array #
var someArray = [SomeType]()
var someArray = [SomeType](repeating: InitialValue, count: NumbeOfElements)
Int
an empty array with a quantity of 3 and an initial value of 0var someInts = [Int](repeating: 0, count: 3)
var someInts:[Int] = [10, 20, 30]
9.26.2. Access array #
var someVar = someArray[index]
index
index starts at 0, that is, index 0 corresponds to the first element, index 1 corresponds to the second element, and so on.import Cocoa
var someInts = [Int](repeating: 10, count: 3)
var someVar = someInts[0]
print( "The value of the first element \(someVar)" )
print( "The value of the first element \(someInts[1])" )
print( "The value of the first element \(someInts[2])" )
The value of the first element is 10
The value of the second element is 10
The value of the third element is 10
9.26.3. Modify array #
append()
method or assignment operator
+=
add elements at the end of the array, as shown below, we initialize an array andadd elements to it:import Cocoa
var someInts = [Int]()
someInts.append(20)
someInts.append(30)
someInts += [40]
var someVar = someInts[0]
print( "The value of the first element is \(someVar)" )
print( "The value of the second element is \(someInts[1])" )
print( "The value of the third element is \(someInts[2])" )
The value of the first element is 20
The value of the second element is 30
The value of the third element is 40
import Cocoa
var someInts = [Int]()
someInts.append(20)
someInts.append(30)
someInts += [40]
// Modify the last element
someInts[2] = 50
var someVar = someInts[0]
print( "The value of the first element is \(someVar)" )
print( "The value of the second element is \(someInts[1])" )
print( "The value of the third element is \(someInts[2])" )
The value of the first element is 20
The value of the second element is 30
The value of the third element is 50
9.26.4. Ergodic array #
for-in
loop through the data items in all the arrays:import Cocoa
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Runoob")
someStrs += ["Google"]
for item in someStrs {
print(item)
}
Apple
Amazon
Runoob
Google
String
of
enumerate()
method to traverse the array. Examples are as follows:import Cocoa
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Runoob")
someStrs += ["Google"]
for (index, item) in someStrs.enumerated() {
print("The value at index=\ (index) position is \(item)")
}
The value at index=0 is Apple
The value at index=1 is Amazon
The value at index=2 is Runoob
The value at index=3 is Google
9.26.5. Merge array #
import Cocoa
var intsA = [Int](repeating: 2, count:2)
var intsB = [Int](repeating: 1, count:3)
var intsC = intsA + intsB
for item in intsC {
print(item)
}
2
2
1
1
1
9.26.6.
count
attribute #
count
property to calculate the number of array elements:import Cocoa
var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = intsA + intsB
print("intsA elements number is \(intsA.count)")
print("intsB elements number is \(intsB.count)")
print("intsC elements number is \(intsC.count)")
intsA elements number is 2
intsB elements number is 3
intsC elements number is 5
9.26.7.
isEmpty
Attribute #
isEmpty
to determine whether the array is empty and return a Boolean value:import Cocoa
var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = [Int]()
print("intsA.isEmpty = \(intsA.isEmpty)")
print("intsB.isEmpty = \(intsB.isEmpty)")
print("intsC.isEmpty = \(intsC.isEmpty)")
intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true