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

filter(_:)

Returns an array containing, in order, the elements of the sequence that satisfy the given predicate. 指定された述部を満足させるものであるシーケンスの要素を、順序通りに、含んでいる配列を返します。

Declaration 宣言

func filter(_ isIncluded: (Base.Element) throws -> Bool) rethrows -> [Base.Element]

Parameters パラメータ

isIncluded

A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the returned array. あるクロージャ、それはシーケンスの1つの要素をそれの引数として取り、その要素が返される配列に含まれるべきかどうかを指し示しているブール値を返します。

Return Value 戻り値

An array of the elements that isIncluded allowed. isIncludedが許可する要素からなる配列。

Discussion 解説

In this example, filter(_:) is used to include only names shorter than five characters. この例において、filter(_:)は5つの文字より短い名前のみ含めるために使われます。


let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let shortNames = cast.filter { $0.count < 5 }
print(shortNames)
// Prints "["Kim", "Karl"]"

Complexity: O(n), where n is the length of the sequence. 計算量:O(n)、ここでnはシーケンスの長さです。