2

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?

Amit Jagesha シ
  • 1,092
  • 12
  • 21
  • Possible duplicate of [Confused about Swift Array Declarations](http://stackoverflow.com/questions/33668949/confused-about-swift-array-declarations) – mclaughj Mar 01 '16 at 15:28

2 Answers2

1

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.

Tabassum Latif
  • 247
  • 2
  • 15
0

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.

Piyush Vaish
  • 107
  • 8