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

filter(_:)

Returns a new collection of the same type containing, in order, the elements of the original collection that satisfy the given predicate. オリジナルのコレクションの要素で与えられた述部を満たすものを、順序正しく、含んでいる同じ型の新しいコレクションを返します。

Declaration 宣言

func filter(_ isIncluded: (UInt8) throws -> Bool) rethrows -> Data

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 collection. あるクロージャ、それは1つの要素をそれの引数として取り、その要素が返されるコレクションに含まれるべきかどうかを指し示しているブール値を返します。

Return Value 戻り値

A collection 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 collection. 計算量:O(n)、ここでnはコレクションの長さです。

See Also 参照

Selecting Bytes バイトを選択する