Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift array syntax using angle brackets

While reading about Big O Notation on a blog, there was a reference to the following line of code:

let numberList : Array<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

It catches you off guard because it has the keyword type Int inside of angle brackets which I've considered this is how generics syntax works. So I investigated in a playground if this line of code equates to what I'm used to seeing:

let numberList : Array<Int> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
let numberList2 : [Int] = [1,2,3,4,5,6,7,8,9,10]
if numberList == numberList2{
    print("They're the same")
}

Before I move forward, I just want to be sure and ask others if this is definitely the same and it doesn't matter which format you choose.

Also I'm thinking this isn't a duplicate because I've clicked on all the swift array links when posing a question before posting this one.

like image 886
Laurence Wingo Avatar asked Jun 01 '26 22:06

Laurence Wingo


1 Answers

Before I move forward, I just want to be sure and ask others if this is definitely the same and it doesn't matter which format you choose.

Yes, they mean the same thing and it doesn't matter which you use. [Int] is pure syntactic sugar for Array<Int>, no more no less.

like image 140
matt Avatar answered Jun 03 '26 11:06

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!