user6510422 user6510422 - 4 months ago 23
Swift Question

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.

Answer

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.

Comments