
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
swift Array insert() Function
The insert() function of an array is used to insert a new element in the given array. It can insert a new element in the existing or a new array. It can insert a single or a range of elements in the given array. It only works for mutating arrays, where mutating arrays are those arrays that can be modified. If we try to use this method with a non-mutating array we will get an error.
For example, we have an array = [10, 20, 30]. We are now using the insert(40, at: 2) function we add 40 at index 2 in the given array. Hence the resultant array is [10, 20, 40, 30].
We can use the insert() function in two different ways −
- insert(_: at:) function
- insert(contentsOf: at:) function
Swift Array insert(_:at:) Function
The insert(_:at:) function is used to insert a new element at the specified index in the given array. This function adds one element at a time. It adds a new element before the existing element at the given index. The complexity of this function is O(n), where n represents the length of the array.
Syntax
Following is the syntax of the insert(_:at:) function −
func insert(_newItem: Element, at x: int)
Parameters
This function takes two parameters:
- newItem − It represents the new element that we want to insert.
- X − It represents the index where we want to insert the new element. Here the index must be a valid index or equal to the endIndex property.
Return Value
This function does not return anything.
Now we will discuss how to use the insert(_:at:) function with the help of the following examples:
Example 1
Swift program to insert a new element in the existing array using insert(_:at:) function.
import Foundation // Defining array of integer type var array1 = [3, 5, 6, 7] print("Original Array:", array1) // Inserting a new element at position 2 // Using insert(_:at:) function array1.insert(10, at:2) print("Updated Array: ", array1) // Defining array of string type var array2 = ["Black", "White", "Blue"] print("\nOriginal Array:", array2) // Inserting a new element at position 1 // Using insert(_:at:) function array2.insert("Pink", at:1) print("Updated Array: ", array2)
Output
Original Array: [3, 5, 6, 7] Updated Array: [3, 5, 10, 6, 7] Original Array: ["Black", "White", "Blue"] Updated Array: ["Black", "Pink", "White", "Blue"]
Example 2
Swift program to insert a new element in the empty array and the mixed type array using insert(_:at:) function.
import Foundation // Defining an empty array of integer type var arr : [Int] = [] print("Original Array:", arr) // Inserting new elements at position 0, 1, and 2 // Using insert(_:at:) function arr.insert(10, at:0) arr.insert(11, at:1) arr.insert(19, at:2) print("Updated Array: ", arr) // Defining an array of mix type var arrMix : [Any] = [10, "Hello"] print("\nOriginal Array:", arrMix) // Inserting a new element at position 1 // Using insert(_:at:) function arrMix.insert(29.2, at:0) print("Updated Array: ", arrMix)
Output
Original Array: [] Updated Array: [10, 11, 19] Original Array: [10, "Hello"] Updated Array: [29.2, 10, "Hello"]
Swift Array insert(contentsOf:at: ) Function
The insert(contentsOf:at:) function is used to insert a range or sequence of elements at the specified index in the given array. Or we can say that it can insert multiple elements at a time. It can also add a new element before the existing element at the given index. The complexity of this function is O(n+m), where n represents the length of the array and m represents the length of the new element.
Syntax
Following is the syntax of the insert(contentsOf:at:) function −
func insert(contentsOf newItem: Collection, at x: Self.Index)
Parameters
This function takes two parameters:
- newItem − It represents the sequence of new elements that we want to insert.
- X − It represents the index where we want to insert the new element. Here the index must be a valid index or equal to the endIndex property.
Return Value
This function does not return anything.
Now we will discuss how to use the insert(contentsOf:at:) function with the help of the following examples:
Example 1
Swift program to insert a range of elements in the given array using insert(contentsOf:at:) function.
import Foundation // Defining an empty array of integer type var arr1 : [Int] = [] print("Original Array:", arr1) // Inserting multiple elements at position 0 // Using insert(contentsOf:at:) function arr1.insert(contentsOf: 1...10, at:0) print("Updated Array: ", arr1) // Defining an array of integer type var arr2 : [Int] = [3, 4, 5, 7] print("\nOriginal Array:", arr2) // Inserting multiple elements at position 3 // Using insert(contentsOf:at:) function arr2.insert(contentsOf: 3..<11, at:3) print("Updated Array: ", arr2)
Output
Original Array: [] Updated Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Original Array: [3, 4, 5, 7] Updated Array: [3, 4, 5, 3, 4, 5, 6, 7, 8, 9, 10, 7]
Example 2
Swift program to add multiple elements in the 2-D array using insert(contentsOf:at:) function.
import Foundation // Function to add a row of elements in the array at specified position func addRow(arr: inout [[Int]], row: [Int], position: Int) { guard position >= 0 && position <= arr.count else { print("Invalid index") return } arr.insert(contentsOf: [row], at: position) } var inputArray = [ [3, 12, 11, 15], [10, 19, 1, 12] ] let newRow = [20, 1, 1, 1] let indexValue = 0 print("Original Array:") for x in inputArray { print(x) } // Calling the above function addRow(arr: &inputArray, row: newRow, position: indexValue) print("\nUpdated array after insertion:") for y in inputArray { print(y) }
Output
Original Array: [3, 12, 11, 15] [10, 19, 1, 12] Updated array after insertion: [20, 1, 1, 1] [3, 12, 11, 15] [10, 19, 1, 12]