Swift Array Declaration Difference

127 views Asked by At

What is the difference between below declaration of array syntax in swift?

 var arr:[Int]

 var arr=Array<Int>()

and which one is better? How and Why?

2

There are 2 answers

0
Piyush Vaish On

var arr:[Int] This one simply declares an array of Integers named arr. It does not initialise the array and hence is not usable.

var arr=Array() This one declares as well as initialises the array. We can add whatever we want to this arr.

The second one is better since its initialised and usable.

0
Tabassum Latif On

var arr:[int] this is fixed size array and don't change size after initialization.

var arr=Array() this is array list and this array change the size with respect to number of elements. you can easily remove and add element easily in this array.