Swift Arrays
Swift arrays store multiple values of the same type in an ordered list. The same value can appear multiple times at different positions in an array.
Swift arrays enforce type checking on elements; an error is reported if the types differ. Swift arrays should follow a form like Array<Element>, where Element is the only data type allowed in the array.
If you create an array and assign it to a variable, the created collection is mutable. This means that after creating the array, you can change the items in the array by adding, removing, or modifying them. If you assign an array to a constant, the array is immutable, and both the size and contents of the array cannot be modified.
Creating Arrays
We can use the initializer syntax to create an empty array of a specific data type:
var someArray = [SomeType]()
The following is the syntax for creating an array with an initialized size:
var someArray = [SomeType](repeating: InitialValue, count: NumbeOfElements)
The following example creates an array of type Int, with a count of 3 and an initial value of 0:
var someInts = [Int](repeating: 0, count: 3)
The following example creates an array containing three elements:
var someInts:[Int] = [10, 20, 30]
Accessing Arrays
We can access the elements of an array based on the array's index, with the syntax as follows:
var someVar = someArray[index]
The index starts from 0; that is, index 0 corresponds to the first element, index 1 corresponds to the second element, and so on.
Through the following example, we can learn how to create, initialize, and access arrays:
import Cocoa var someInts = [Int](repeating: 10, count: 3) var someVar = someInts[0] print( "第一个元素的值 \(someVar!)" ) print( "第二个元素的值 \(someInts[1]!)" ) print( "第三个元素的值 \(someInts[2]!)" )
The output of the above program execution is:
第一个元素的值 10 第二个元素的值 10 第三个元素的值 10
Modifying Arrays
You can use the append() method or the assignment operator += to add elements at the end of an array. As shown below, we initialize an array and add elements to it:
import Cocoa var someInts = [Int]() someInts.append(20) someInts.append(30) someInts += [40] var someVar = someInts[0] print( "第一个元素的值 \(someVar)" ) print( "第二个元素的值 \(someInts[1])" ) print( "第三个元素的值 \(someInts[2])" )
The output of the above program execution is:
第一个元素的值 20 第二个元素的值 30 第三个元素的值 40
We can also modify the value of array elements by index:
import Cocoa var someInts = [Int]() someInts.append(20) someInts.append(30) someInts += [40] // 修改最后一个元素 someInts[2] = 50 var someVar = someInts[0] print( "第一个元素的值 \(someVar)" ) print( "第二个元素的值 \(someInts[1])" ) print( "第三个元素的值 \(someInts[2])" )
The output of the above program execution is:
第一个元素的值 20 第二个元素的值 30 第三个元素的值 50
Iterating Over Arrays
We can use a for-in loop to iterate over all the data items in the array:
import Cocoa
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Example")
someStrs += ["Google"]
for item in someStrs {
print(item)
}
The output of the above program execution is:
Apple Amazon Example Google
If we need both the value and the index of each data item, we can use the enumerated() method of String to iterate over the array. An example is as follows:
import Cocoa
var someStrs = [String]()
someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Example")
someStrs += ["Google"]
for (index, item) in someStrs.enumerated() {
print("在 index = \(index) 位置上的值为 \(item)")
}
The output of the above program execution is:
在 index = 0 位置上的值为 Apple 在 index = 1 位置上的值为 Amazon 在 index = 2 位置上的值为 Example 在 index = 3 位置上的值为 Google
Combining Arrays
We can use the addition operator (+) to combine two existing arrays of the same type. The data type of the new array will be inferred from the data types of the two arrays:
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)
}
The output of the above program execution is:
2 2 1 1 1
count Property
We can use the count property to calculate the number of elements in the array:
import Cocoa
var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = intsA + intsB
print("intsA 元素个数为 \(intsA.count)")
print("intsB 元素个数为 \(intsB.count)")
print("intsC 元素个数为 \(intsC.count)")
The output of the above program execution is:
intsA 元素个数为 2 intsB 元素个数为 3 intsC 元素个数为 5
isEmpty Property
We can use the read-only property isEmpty to determine whether the array is empty; it returns 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)")
The output of the above program execution is:
intsA.isEmpty = false intsB.isEmpty = false intsC.isEmpty = trueOther Extensions