如何在苹果的新语言Swift中取消设置/删除数组中的元素?
下面是一些代码:
let animals = ["cats", "dogs", "chimps", "moose"]
如何将元素animals[2]从数组中移除?
如何在苹果的新语言Swift中取消设置/删除数组中的元素?
下面是一些代码:
let animals = ["cats", "dogs", "chimps", "moose"]
如何将元素animals[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)!)
}
}
}
其他回答
使用索引数组删除元素:
Array of Strings and indexes let animals = ["cats", "dogs", "chimps", "moose", "squarrel", "cow"] let indexAnimals = [0, 3, 4] let arrayRemainingAnimals = animals .enumerated() .filter { !indexAnimals.contains($0.offset) } .map { $0.element } print(arrayRemainingAnimals) //result - ["dogs", "chimps", "cow"] Array of Integers and indexes var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] let indexesToRemove = [3, 5, 8, 12] numbers = numbers .enumerated() .filter { !indexesToRemove.contains($0.offset) } .map { $0.element } print(numbers) //result - [0, 1, 2, 4, 6, 7, 9, 10, 11]
使用另一个数组的元素值删除元素
整数数组 let arrayResult = numbers。筛选器{元素 返回! indexesToRemove.contains(元素) } 打印(arrayResult) //result - [0,1,2,4,6,7,9,10,11] 字符串数组 让arrayLetters =(“a”、“b”、“c”,“d”,“e”,“f”,“g”,“h”,“我”) let arrayRemoveLetters = ["a", "e", "g", "h"] let arrayRemainingLetters = arrayLetters。过滤器{ ! arrayRemoveLetters.contains (0) } 打印(arrayRemainingLetters) //result - ["b", "c", "d", "f", "i"]
我使用这个扩展,几乎与Varun的一样,但这一个(下面)是万能的:
extension Array where Element: Equatable {
mutating func delete(element: Iterator.Element) {
self = self.filter{$0 != element }
}
}
斯威夫特5
guard let index = orders.firstIndex(of: videoID) else { return }
orders.remove(at: index)
斯威夫特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]
Swift中很少涉及数组操作
创建数组
var stringArray = ["One", "Two", "Three", "Four"]
在数组中添加对象
stringArray = stringArray + ["Five"]
从索引对象中获取值
let x = stringArray[1]
添加对象
stringArray.append("At last position")
在索引处插入对象
stringArray.insert("Going", at: 1)
删除对象
stringArray.remove(at: 3)
Concat对象值
var string = "Concate Two object of Array \(stringArray[1]) + \(stringArray[2])"