How to remove an element from an array in Swift

后端 未结 18 2144
灰色年华
灰色年华 2020-11-28 18:38

How can I unset/remove an element from an array in Apple\'s new language Swift?

Here\'s some code:

let animals = [\"cats\", \"dogs\", \"chimps\", \"m         


        
18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 18:48

    To remove elements from an array, use the remove(at:), removeLast() and removeAll().

    yourArray = [1,2,3,4]
    

    Remove the value at 2 position

    yourArray.remove(at: 2)
    

    Remove the last value from array

    yourArray.removeLast()
    

    Removes all members from the set

    yourArray.removeAll()
    

提交回复
热议问题