Amit Jagesha シ
Amit Jagesha シ

Reputation: 1112

Swift Array Declaration Difference

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?

Upvotes: 2

Views: 127

Answers (2)

Piyush Vaish
Piyush Vaish

Reputation: 107

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.

Upvotes: 0

Tabassum Latif
Tabassum Latif

Reputation: 257

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.

Upvotes: 1

Related Questions