4
6

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 5 years have passed since last update.

[Swift] 同じ要素を繰り返す、Repeated

Posted at

以前Sequenceに関するこんな記事を書きました。

[Swift] シンプルなカウントアップでSequenceに強くなる

カウントアップと微妙に違ったので書けなかったのですが、似たようなSequenceの仲間でRepeatedというのがあります。

RepeatedはrepeateElement(_,count:)関数で生成します。

let manyApples = repeatElement("🍎", count: 100)
for apple in manyApples {
    print("\(apple)") // 🍎, 🍎, 🍎...
}

お察しの通り、Repeatedは第一引数で指定した要素を指定した回数繰り返すものです。RepeatedはRandomAccessCollectionなので、ランダムアクセスも可能です。

うまい使い所が難しいですが、使えると気持ちよさそうですね。


:whale2: Twitter: @a_beco
:guitar: SoundCloud: @beco <- 作業用BGMにどうぞ!

4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?