To remove the first element of an array arr
in Swift, you can use the removeFirst()
method. This method removes the first element of the array and returns it.
main.swift115 chars4 lines
Note that removeFirst()
is a mutating method, which means it modifies the array in place. If you don't need the removed element, you can simply call removeFirst()
without assigning its return value to a variable:
main.swift112 chars4 lines
If the array is empty when you try to remove the first element, calling removeFirst()
will result in a runtime error. To avoid this, you can use the first
property to check if the array is not empty before removing the first element:
main.swift182 chars7 lines
gistlibby LogSnag