I want to be able to convert a whole int to an array of elements. So If the int is 4 I want to be able to convert it to an array with 4 elements. Like so:
var num = 4
var arr = [1,2,3,4]
You could use an Int's array initializer with a range.
Either:
let arr = [Int](1...4)
or:
let arr = Array(1...4)
Result:
[1, 2, 3, 4]
let num = 4
let arr = Array(1...num)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With