Latest web development tutorials

Swift array

Swift array using a plurality of values ​​stored in an ordered list of the same type. The same value can appear multiple times in different locations in an array.

Swift array of forces detected the type of element, if different types is given, Swift array should follow the form such as Array <Element>, where Element is only permitted in this array data types exist.

If you create an array and assigned to a variable, create a collection that can be modified. This means that when you create an array, you can add, delete, modify, change the way the array of items. If you assign a constant array, the array will not be changed, and the size and content of the array can not be modified.


Creating an array

We can use the constructor syntax to create an empty array consisting of a specific data type:

var someArray = [SomeType]()

The following is to create an initial size of the array syntax:

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

The following example creates a type of Int, size 3, the initial value of 0 empty array:

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

The following example creates an array with three elements:

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

Access Array

We can access elements of the array according to the index of the array, the syntax is as follows:

var someVar = someArray[index]

index Index starts at 0, and the index of 0 corresponds to the first element, the index 1 corresponds to the second element, and so on.

By the following examples, we can learn how to create, initialize, access the array:

import Cocoa

var someInts = [Int](count: 3, repeatedValue: 10)

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

The above program execution output is:

第一个元素的值 10
第二个元素的值 10
第三个元素的值 10

Modify the array

You can use append () method or the assignment operator + = add elements to the end of the array, as shown below, we initialize an array, and add elements:

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 above program execution output is:

第一个元素的值 20
第二个元素的值 30
第三个元素的值 40

We can also modify the value of the array element indexed by:

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 above program execution output is:

第一个元素的值 20
第二个元素的值 30
第三个元素的值 50

Iterate

We can use the for-in loop to iterate over all the items in the array:

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("w3big")
someStrs += ["Google"]

for item in someStrs {
   print(item)
}

The above program execution output is:

Apple
Amazon
w3big
Google

If we also need values ​​and index values ​​for each data item, you can use the String enumerate () method to traverse the array. Examples are as follows:

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("w3big")
someStrs += ["Google"]

for (index, item) in someStrs.enumerate() {
   print("在 index = \(index) 位置上的值为 \(item)")
}

The above program execution output is:

在 index = 0 位置上的值为 Apple
在 index = 1 位置上的值为 Amazon
在 index = 2 位置上的值为 w3big
在 index = 3 位置上的值为 Google

Merge array

We can use the addition operator (+) to merge the same two types of array that already exists. Data type of the new array will be inferred from the data types of the two arrays:

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)

var intsC = intsA + intsB

for item in intsC {
   print(item)
}

The above program execution output is:

2
2
1
1
1

count property

We can use the 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 元素个数为 \(intsA.count)")
print("intsB 元素个数为 \(intsB.count)")
print("intsC 元素个数为 \(intsC.count)")

The above program execution output is:

intsA 元素个数为 2
intsB 元素个数为 3
intsC 元素个数为 5

isEmpty property

We can read-only attribute isEmpty to determine whether the array is empty, 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 above program execution output is:

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