0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Swiftで配列をmapで扱う。

Last updated at Posted at 2022-09-09

mapは配列の要素すべてに、何らかの処理を施して返すもの。

例えば, 指定された季節に応じて、その時の花の写真を出し分けたいという状況を考えてみる。
画像ファイルの名前は「花の名前+季節」
夏のひまわりだったら、「sumflowerSummer」みたいなイメージ。

let flowersImage = ["rose", "cherryBlossoms", "sunflower"]

print (springFlowers)
/// ["rose", "cherryBlossoms", "sunflower"]

let springFlowers = flowersImage.map { $0 + "Spring" }

print (springFlowers)
/// ["roseSpring", "cherryBlossomsSpring", "sunflowerSpring"]

ここで、$0は引数の省略。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?