如何在没有索引值的情况下从数组中删除元素?

时间:2016-07-12 17:30:57

标签: ios arrays swift

有没有人知道如何从数组中删除元素而不确切知道它在哪个位置?

var array = ["A", "B", "C"]

如何删除" A"如果整个数组中只有少数几个并且它包含数千个字符串(只需删除一个" A"不是全部)?

1 个答案:

答案 0 :(得分:3)

就像这样:

var array = ["A", "B", "C"]

if let firstIndex = array.indexOf("A") { // Get the first index of "A"
    array.removeAtIndex(firstIndex) // Remove element at that index
}

斯威夫特3:

if let firstIndex = array.index(of: "A") {
    array.remove(at: firstIndex)
}