Instance Method インスタンスメソッド

dropLast(_:)

Returns a sequence containing all but the given number of final elements.

Declaration 宣言

func dropLast(_ k: Int = 1) -> [Self.Element]

Return Value 戻り値

A sequence leaving off the specified number of elements.

Parameters パラメータ

n

The number of elements to drop off the end of the sequence. n must be greater than or equal to zero.

Discussion 議論

The sequence must be finite. If the number of elements to drop exceeds the number of elements in the sequence, the result is an empty sequence.


let numbers = [1, 2, 3, 4, 5]
print(numbers.dropLast(2))
// Prints "[1, 2, 3]"
print(numbers.dropLast(10))
// Prints "[]"