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

下面是一些代码:

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

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


当前回答

鉴于

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

其他回答

鉴于

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

let关键字用于声明不能更改的常量。如果你想修改一个变量,你应该使用var代替,例如:

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

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

一个保持原始集合不变的非突变替代方法是使用过滤器创建一个新的集合,而不删除你想要的元素,例如:

let pets = animals.filter { $0 != "chimps" }

你可以这么做。首先确保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)!)
        }
    }
}

斯威夫特5

guard let index = orders.firstIndex(of: videoID) else { return }
orders.remove(at: index)

我提出了以下扩展,负责从数组中删除元素,假设数组中的元素实现了Equatable:

extension Array where Element: Equatable {
  
  mutating func removeEqualItems(_ item: Element) {
    self = self.filter { (currentItem: Element) -> Bool in
      return currentItem != item
    }
  }

  mutating func removeFirstEqualItem(_ item: Element) {
    guard var currentItem = self.first else { return }
    var index = 0
    while currentItem != item {
      index += 1
      currentItem = self[index]
    }
    self.remove(at: index)
  }
  
}
  

用法:

var test1 = [1, 2, 1, 2]
test1.removeEqualItems(2) // [1, 1]

var test2 = [1, 2, 1, 2]
test2.removeFirstEqualItem(2) // [1, 1, 2]