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 ...

Get Swift Quick Syntax Reference 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.