O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter    20

Arrays

To organize a list of variables, constants, and other types, you can use arrays. All items in an array must be of the same type, and you can have lists of items such as integers, floating-point numbers, strings, and objects. Listing 20-1 shows two ways to create empty arrays that you can add items to later.

Listing 20-1. Creating Arrays

var a1:Array<String> = Array<String>()var a2:[String] = [String]()

The first line in Listing 20-1 shows you the long way of declaring an array. You must use the Array keyword and include the type that is stored in the array between the less-than (<) and greater-than (>) signs. In Listing 20-1, a1 is an array that can hold a list of strings.

You must also use the assignment operator (=) to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required