如何在苹果的新语言Swift中取消设置/删除数组中的元素?

下面是一些代码:

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

如何将元素animals[2]从数组中移除?


当前回答

斯威夫特5: 这是一个很酷的和简单的扩展来删除数组中的元素,而不需要过滤:

   extension Array where Element: Equatable {

    // Remove first collection element that is equal to the given `object`:
    mutating func remove(object: Element) {
        guard let index = firstIndex(of: object) else {return}
        remove(at: index)
    }

}

用法:

var myArray = ["cat", "barbecue", "pancake", "frog"]
let objectToRemove = "cat"

myArray.remove(object: objectToRemove) // ["barbecue", "pancake", "frog"]

也适用于其他类型,例如Int,因为Element是泛型类型:

var myArray = [4, 8, 17, 6, 2]
let objectToRemove = 17

myArray.remove(object: objectToRemove) // [4, 8, 6, 2]

其他回答

你可以这么做。首先确保Dog确实存在于数组中,然后删除它。如果您认为Dog可能在数组中发生多次,则添加for语句。

var animals = ["Dog", "Cat", "Mouse", "Dog"]
let animalToRemove = "Dog"

for object in animals {
    if object == animalToRemove {
        animals.remove(at: animals.firstIndex(of: animalToRemove)!)
    }
}

如果你确定Dog在数组中退出并且只发生了一次,那么就这样做:

animals.remove(at: animals.firstIndex(of: animalToRemove)!)

如果两者都有,字符串和数字

var array = [12, 23, "Dog", 78, 23]
let numberToRemove = 23
let animalToRemove = "Dog"

for object in array {

    if object is Int {
        // this will deal with integer. You can change to Float, Bool, etc...
        if object == numberToRemove {
        array.remove(at: array.firstIndex(of: numberToRemove)!)
        }
    }
    if object is String {
        // this will deal with strings
        if object == animalToRemove {
        array.remove(at: array.firstIndex(of: animalToRemove)!)
        }
    }
}

鉴于

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

删除第一个元素

animals.removeFirst() // "cats"
print(animals)        // ["dogs", "chimps", "moose"]

删除最后一个元素

animals.removeLast() // "moose"
print(animals)       // ["cats", "dogs", "chimps"]

删除索引处的元素

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

删除未知索引的元素

只针对一个元素

if let index = animals.firstIndex(of: "chimps") {
    animals.remove(at: index)
}
print(animals) // ["cats", "dogs", "moose"]

对于多个元素

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

animals = animals.filter(){$0 != "chimps"}
print(animals) // ["cats", "dogs", "moose"]

笔记

上述方法就地修改数组(过滤器除外)并返回被删除的元素。 快速指南地图滤镜减少 如果不想修改原始数组,可以使用dropFirst或dropLast创建一个新数组。

更新至Swift 5.2

扩展删除字符串对象

extension Array {
    mutating func delete(element: String) {
        self = self.filter() { $0 as! String != element }
    }
}

上面的答案似乎假定您知道要删除的元素的索引。

通常,您知道对数组中要删除的对象的引用。在这种情况下,直接使用对象引用可能会更容易,而不必到处传递它的索引。因此,我建议这个解决方案。它使用标识符!==,用于测试两个对象引用是否都引用同一个对象实例。

func delete(element: String) {
    list = list.filter { $0 != element }
}

当然,这不仅仅适用于字符串。

如果你有一个自定义对象数组,你可以像这样通过特定的属性进行搜索:

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

或者如果你想通过名字来搜索

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