SoFunction
Updated on 2025-04-06

Swift arrays and common methods detailed explanations

Swift arrays and common methods

1. Create an array

// Create an integer arrayvar array1: [Int] = [] // []
var arrya2: Array<Int> = [1, 2, 3] // [1, 2, 3]
var arryaInt = [1, 2, 3] // [1, 2, 3]
var array3 = Array(arrayLiteral: 1, 2, 3) // [1, 2, 3]

2. Quickly create an array of duplicate elements

var array4 = Array(repeating: "swift", count: 3) // ["swift", "swift", "swift"]
var array5 = Array(repeating: 1001, count: 3) // [1001, 1001, 1001]

3. Add up the array

// Add 2 arrays of the same typevar array6 = [1, 2, 3] + [4, 5, 6] // [1, 2, 3, 4, 5, 6]

4. Common methods

// When the array is declared to be variable, you can use methods such as adding, deleting, and modifying. Constant arrays cannot be modified related operations.var array = [1, 2, 3, 4, 5, 6, 7, 8]
print() // 8

// Determine that the array is an empty arrayif  {
    print("array is empty")
} else {
    print("array is not empty")
}

// Access elements through subscriptvar ele = array[1] // 2

// Intercept new arrayvar subArray = array[1...2] // [2, 3]

// Get the first elementvar firstEle =  // 1

// Get the last elementvar lastEle =  // 8

// Modify the elements corresponding to the subscriptarray[1] = 22
array // [1, 22, 3, 4, 5, 6, 7, 8]

// Modify elements in the specified rangearray[0...2] = [1, 2, 3] // [1, 2, 3]
 
// Append a single element(9) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

// Add a set of elements(contentsOf: [10, 11, 12]) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

// Insert a single element at the specified location(0, at: 0) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

// Insert a set of elements at the specified location(contentsOf: [-3, -2, -1], at: 0) // [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

// Remove the specified element(at: 1) // -2

// Remove a set of elements(0...2) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

// Remove the first element() // 1

// Remove the end element() // 12

// Remove the first few elements(3) // [5, 6, 7, 8, 9, 10, 11]

// Remove the last elements(3) // [5, 6, 7, 8]

// Replace elements in the specified range(0...3, with: [1, 2, 3, 4]) // [1, 2, 3, 4]

// Determine that the specified element is includedif (3) {
    print("array contains 3")
}

// Remove all elements() // []

var sortArr = [2, 1, 3, -1]

//Sorting from small to large(by: <) // [-1, 1, 2, 3]

// Sort from large to small(by: >) // [3, 2, 1, -1]

// Get the maximum value of the array() // -1

// Get the minimum value of the array() // 3

5. Array traversal

let arr = [11, 22, 33]

for item in arr {
    print(item)
}

// Print the subscript and corresponding elements of the arrayfor item in () {
    print(item) // (offset: 0, element: 11) (offset: 1, element: 22) (offset: 2, element: 33)
}

// Subscript traversalfor index in  {
    print(arr[index])
}

GitHub source code:

This is the end of this article about detailed explanations and summary of Swift arrays and common methods. For more related Swift array content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!