How to remove an element from an array in Swift

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

    If you have array of custom Objects, you can search by specific property like this:

    if let index = doctorsInArea.firstIndex(where: {$0.id == doctor.id}){
        doctorsInArea.remove(at: index)
    }
    

    or if you want to search by name for example

    if let index = doctorsInArea.firstIndex(where: {$0.name == doctor.name}){
        doctorsInArea.remove(at: index)
    }
    

提交回复
热议问题