1
0

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】Array(repeating:count:)の使い方

Posted at

はじめに

Array(repeating:count:)は、同じ値を指定した回数分繰り返して配列を生成する機能です。この機能を使用することで、簡単に初期値が設定された配列を生成することができます。

使用方法

以下のように、 repeatingに値を指定し、countに回数を指定することで初期値付きの配列が生成されます。

// [0, 0, 0, 0, 0]
let array = Array(repeating: 0, count: 5)

上記のコードは、 05 回繰り返した配列を生成し、arrayに代入しています。

型を明示する場合

型を明示する場合は、このように書きます。

// ["default", "default", "default"]
let stringArray = [String](repeating: "default", count: 3)

上記のコードは、 String型の "default"3 回繰り返した配列を生成し、stringArrayに代入しています。

まとめ

Array(repeating:count:)を使用することで、同じ値を指定した回数分繰り返した配列を簡単に生成することができます。


ご覧いただきありがとうございました。

🐦 Twitter: @shota_appdev

📱 個人アプリ: Symbols Explorer

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?