Swift Array – Delete Last Element
To delete last element of an Array in Swift, call dropLast()
method on the array. dropLast()
method without any value for argument, returns a new array with last element dropped from the trailing end of given array.
The syntax to call dropLast()
method on the array is
</>
Copy
var result = arr.dropLast()
Example
In the following program, we will initialize an array with some values, and delete the last element using dropLast()
method.
main.swift
</>
Copy
var arr = [1, 99, 6, 74, 5]
let result = arr.dropLast()
print("Original Array: \(arr)")
print("Resulting Array: \(result)")
Output
Conclusion
In this Swift Tutorial, we learned how to get the subsequence which contains the elements of given array, with the last element dropped.