Create dynamic array of size swift
I want to create an array if I do this how it works:
var arrayEingabe = Array(count:30, repeatedValue:0)
If I do like this, it doesn't work:
var sizeArray = 30
var arrayEingabe = Array(count:sizeArray, repeatedValue:0)
In the end, I want to resize my array based on what the user has entered.
I searched the internet for one hour, but I couldn't find an answer.
Thanks for the help guys
greets
Kova
+3
source to share
2 answers
Actually both examples compiled well for me, but you should be more type specific. Something like:
var arrayCount:Int = 30
var arrayEingabe = Array(count:arrayCount, repeatedValue:Int())
in fact this might be better for you:
var arrayEingabe = [Int]()
This creates an empty array, and as mentioned in the comments, Swift arrays are mutable. You can add, replace and remove items as you like.
+8
source to share