LoginSignup
6
6

More than 5 years have passed since last update.

初期化時に連番の配列を1行で生成する

Last updated at Posted at 2016-01-26

やりたかったこと

配列の初期化時に [1,2,3,4,5] みたいな連番の配列を一括生成したかったけど、案外と1行で出来る文法がなかった

こうする

```swift let array = [Int]() + (1...5) print(array) // [1, 2, 3, 4, 5] ``` そんだけ!

コメントで教えてもらったけど、そもそもこうやってかけるんだって!

let array = [Int](1...5)
print(array) // [1, 2, 3, 4, 5]

単にググり力が足りないだけだったようだ。

6
6
2

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