How to remove an element from an array in Swift

后端 未结 18 2172
灰色年华
灰色年华 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:45

    If you don't know the index of the element that you want to remove, and the element is conform the Equatable protocol, you can do:

    animals.remove(at: animals.firstIndex(of: "dogs")!)
    

    See Equatable protocol answer:How do I do indexOfObject or a proper containsObject

提交回复
热议问题