2
3

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

【Swift】数字が連番になった配列を作成する

Posted at

数字が連番する配列を表示したい場合に、一個ずつ入力していくのは面倒臭いですよね、、

let numbers = [1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

##範囲演算子を使って解決!

###...演算子 (終了の値も含む)

let numbers = Array(1...100)

print(numbers.count) //出力 100
print(numbers) // 出力 [1, 2, 3, 4, 5, 6...100]

###..<演算子 (終了の値を含まない)

let numbers = Array(1..<100)

print(numbers.count) //出力 99
print(numbers) // 出力 [1, 2, 3, 4, 5, 6...99]

##おまけ
Array(repeating:count:)というメソッドもあって、これなら同じ値が複数入った配列を生成出来ます。

let bunchOfLove = Array(repeating: "大好き", count: 100)

print(bunchOfLove.count) //出力 100
print(bunchOfLove) // 出力 [大好き, 大好き, 大好き, 大好き...大好き]

❤️
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?