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
kove
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
jwlaughton
source
to share
In Swift 3.0.2: - You can use array initialization method below: -
override init(){
let array = Array(repeating:-1, count:6)
}
Here, repeating: is the default for the array. count: - the number of arrays.
0
Vinod supnekar
source
to share