A Quicker Array

Of course you know from Chapter 1, “Getting Your Feet Wet,” that Swift has powerful type inference. You don’t need to declare an array as verbosely as you did earlier. Here is a quicker way:

var quickerArray = [Int]()

Here you use Int surrounded by square brackets to mean “an array of Ints.” This syntax was originally written as Int[] before Xcode beta 3. It was subsequently changed to include the square brackets around the type. If you write it in the old-fashioned way, Xcode will provide an auto-correction for you.

You can also instantiate an array with items directly in it. When you do this, Swift can infer the type of the array so you don’t even have to declare a type. Here’s how it works:

var arrayOfInts = [1,2,3,4]

This ...

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.