Swift array


Release date:2023-11-30 Update date:2023-12-08 Editor:admin View counts:251

Label:

Swift array

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.

Create an array

We can use construction syntax to create an empty array of specific data types:

var someArray = [SomeType]()

The following is the syntax for creating an initialized size array:

var someArray = [SomeType](repeating: InitialValue, count: NumbeOfElements)

The following example creates a type of Int an empty array with a quantity of 3 and an initial value of 0

var someInts = [Int](repeating: 0, count: 3)

The following example creates an array of three elements:

var someInts:[Int] = [10, 20, 30]

Access array

We can access the elements of the array based on the index of the array. Thesyntax is as follows:

var someVar = someArray[index]

The index index starts at 0, that is, index 0 corresponds to the first element, index 1 corresponds to the second element, and so on.

We can learn how to create, initialize, and access arrays through the following examples:

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 output of the above program execution is as follows:

The value of the first element is 10
The value of the second element is 10
The value of the third element is 10

Modify array

You can use it. 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 output of the above program execution is as follows:

The value of the first element is 20
The value of the second element is 30
The value of the third element is 40

We can also modify the value of the array element through the index:

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 output of the above program execution is as follows:

The value of the first element is 20
The value of the second element is 30
The value of the third element is 50

Ergodic array

We can use it. 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)
}

The output of the above program execution is as follows:

Apple
Amazon
Runoob
Google

If we need both the value and the index value of each data item, we can use the 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 output of the above program execution is as follows:

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

Merge array

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:

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 as follows:

2
2
1
1
1

count attribute

We can use 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)")

The output of the above program execution is as follows:

intsA elements number is 2
intsB elements number is 3
intsC elements number is 5

isEmpty Attribute

We can use the read-only property 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)")

The output of the above program execution is as follows:

intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true

Powered by TorCMS (https://github.com/bukun/TorCMS).