Is there a way to instantly generate an array filled with a range of values in Swift?
You can create a range and map it into an array:
var array = (0...30).map { $0 }
The map
closure simply returns the range element, resulting in an array whose elements are all integers included in the range. Of course it's possible to generate different element and types, such as:
var array = (0...30).map { "Index\($0)" }
which generates an array of strings Index0
, Index1
, etc.