Your First Array

You can create an array by declaring a variable to hold the array and then telling Swift exactly what is going to be in that array. Here’s how you do that:

var myFirstArray:Array<Int> = Array<Int>()

Here you declare the variable myFirstArray, and you declare the variable to be of type Array<Int>. This says that the array is an array of integers. You then set myFirstArray equal to a new array of integers by adding an empty set of parentheses at the end.

Note

Of course you can write this verbose array syntax, but so you will very rarely see arrays written this way because Swift has type inference.

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.