How to Remove First Element from Swift Array?
How to Remove First Element from Swift Array?
Swift Array ¨C Remove First Element
To remove first element from Array in Swift, call remove(at:) method on this array and pass the index 0 for
at parameter. Or call removeFirst() method on the array.
remove(at:) method removes the element at given index.
The syntax to call remove(at:) method on the array with index is
arrayName.remove(at:index)
In this particular scenario, where we would like to remove the first element, the value of index we pass is 0 .
The syntax to call removeFirst() method on the array is
arrayName.removeFirst()
removeFirst() method removes and returns the first element of the array.
Examples
In the following program, we will take an array, and remove the first element using remove(at:) method.
main.swift
var fruits = ["apple", "banana", "cherry", "mango", "guava"]
fruits.remove(at:0)
print(fruits)
Output
The first element "apple" has been removed from the array fruits .
If the array is empty, removing first element using remove(at:) causes a runtime error as shown in the
following example.
main.swift
var fruits: [String] = []
fruits.remove(at:0)
print(fruits)
Output
Remove First Element using Array.removeFirst()
In the following example, we will use removeFirst() method and remove the first element in the array.
main.swift
var fruits = ["apple", "banana", "cherry", "mango", "guava"]
fruits.removeFirst()
print(fruits)
Output
Conclusion
In this Swift Tutorial, we learned how to remove first element from array in Swift.
Swift Tutorial
?
Swift Tutorial
?
Swift Keywords
?
Swift Comments
?
Swift If
?
Swift If-Else
?
Swift For Loop
?
Swift While Loop
?
Swift forEach
?
Swift Repeat While Loop
?
Swift Break
?
Swift Continue
?
Swift Tuple
?
Swift Enum
?
Swift Structure
Strings
?
Swift - Substring
?
Swift - Concatenate Strings
Arrays
?
Swift Array Initialization
?
Swift Print Array
?
Swift Integer Array
?
Swift append Integer to Array
?
Swift String Array
?
Swift append String to Array
?
Swift Get Array Size - count
?
Swift Remove an Element from Array
?
Swift Append / Concatenate Arrays
?
Swift Check if an Array is Empty
Dictionaries
?
Swift Dictionary
?
Swift - Create Dictionary
?
Swift - Create Dictionary using Arrays
?
Swift - Iterate through Dictionary
?
Swift - Get Dictionary Size
?
Swift - Check if Dictionary is Empty
?
Swift - Add or Append Element to Dictionary
?
Swift - Get value using key in Dictionary
?
Swift - Check if a key is present in Dictionary
?
Swift - Merge Two Dictionaries
?
Swift - Convert Dictionary into Arrays of Keys and Values
?
Swift - Print all Keys in a Dictionary
Sets
?
Swift Print Set
?
Swift Get Set Size
?
Swift Insert Element to Set
?
Swift Check if Element is present in Set
File Operations
?
Swift Read Text File
Swift Errors [Solved]
?
Swift error: return from initializer without initializing all stored properties
?
Swift ¨C struct error: missing argument labels in call
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python list remove and return first element
- linked lists colorado state university
- stacks queues and deques
- built in functions university of washington
- python 3 cheat sheet limsi
- elements of programming interviews
- linked list iit kgp
- python cheat sheet
- google spreadsheet split first element
- python tutorial list and negative indexing iit guwahati
Related searches
- how to remove thc from your system
- how to remove items from start menu
- how to remove meth from your system
- how to remove password from computer
- how to remove passwords from my computer
- how to remove password from windows 10
- how to remove element from list python
- how to remove water from gasoline
- how to remove plaque from arteries naturally
- how to remove thc from your body
- how to remove remote management from ipad
- how to remove recent file from photo