Answers for "how to remove item from list swift"

0

how to remove item from list swift

var animals = ["cats", "dogs", "chimps", "moose"]

// remove at specific index
animals.remove(at: 2)  //["cats", "dogs", "moose"]

// remove first element
animals.removeFirst() //["dogs", "moose"]

// remove last element
animals.removeLast() //["dogs"]

// remove at unknown index
if let index = animals.firstIndex(of: "dogs") {
    animals.remove(at: index)
}
Posted by: Guest on November-14-2021

Code answers related to "how to remove item from list swift"

Code answers related to "Swift"

Browse Popular Code Answers by Language