Generic Instance Method 総称体インスタンスメソッド

map(_:)

Returns an array containing the results of mapping the given closure over the sequence’s elements. 与えられたクロージャをシーケンスのもつ要素全体にわたってマップする結果を含んでいる配列を返します。

Declaration 宣言

func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]

Parameters パラメータ

transform

A mapping closure. transform accepts an element of this sequence as its parameter and returns a transformed value of the same or of a different type. マップを行うクロージャ。transformはこのシーケンスのひとつの要素をそれのパラメータとして受け取り、同じもしくは異なる型の変換された値を返します。

Return Value 戻り値

An array containing the transformed elements of this sequence. このシーケンスの変換済み要素を含んでいる配列。

Discussion 解説

In this example, map is used first to convert the names in the array to lowercase strings and then to count their characters. この例では、mapは最初に配列中の名前を小文字文字列に変換するために、それからそれらの文字を数えるために使われます。


let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
// 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"]
let letterCounts = cast.map { $0.count }
// 'letterCounts' == [6, 6, 3, 4]

See Also 参照

Transforming an Array 配列の変形