Array
in Swift有几个排除元素的实例方法,比如dropFirst()
, dropLast()
, drop(while:)
等等。 drop(at:)
呢?
注意:我会使用 remove(at:)
,但我正在使用的数组是一个 let
常量。
最佳答案
Note: I'd use
remove(at:)
, but the array I'm working with is a constant.
我不会让那阻止你:
extension Array {
func drop(at:Int) -> [Element] {
var temp = self
temp.remove(at: at)
return temp
}
}
let numbers = [1, 2, 3, 4, 5]
print(numbers.drop(at: 2)) // [1, 2, 4, 5]
关于arrays - Swift Array 实例方法 drop(at : Int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52350697/