Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create dynamic size array swift

I want to create an Array, if i make it like this it works:

var arrayEingabe = Array(count:30, repeatedValue:0)

If i make it like this it does not work:

var sizeArray = 30
var arrayEingabe = Array(count:sizeArray, repeatedValue:0)

At the end i want to change the size of my Array depending on what the user typed in.

I was searching the web for one hour now, but i could not find the answer.

Thanks for your help guys

Greets

Kove

like image 269
kove Avatar asked Dec 17 '14 09:12

kove


1 Answers

Actually both your examples compiled OK for me, but you should be more specific about types. Something like:

var arrayCount:Int = 30
var arrayEingabe = Array(count:arrayCount, repeatedValue:Int())

actually 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 delete members as you want.

like image 167
jwlaughton Avatar answered Oct 10 '22 09:10

jwlaughton