Mastering Swift: tips about array and dictionary literals

Imagine you have a bunch of delicious candies 🍬, about 20-30 items. Normally a person can eat about 2 or 3 candies per day.

To deal easier with a lot of candies, it makes sense to keep them in a single place. And reasonable this place is a plastic bag or a small box, instead of keeping them spread around and messy on the table.

When you want to take the candies from home to work (to delight tedious working hours 😀), you definitely need a bag.

The same happens when dealing with a bunch of similar objects in an application. The comfort of storing and manipulating the objects grouped in a collection makes this structure so advantageous.

Swift infers the array type as [String] based on collection parameter declaration countElements(of collection: [String]). So the literal [] is safely used on function countElements(of: []) and denotes an empty array of strings.

1.2 Creating an array with values

The array literal

Sometimes the array must include a list of predefined elements. Swift provides an array literal: the initial elements are comma separated and enclosed in square brackets: [element1, element2, ..., elementN].

0...3 instantiates an interval of numbers from 0 to 3. The interval conforms to Sequence protocol.
The call of initializer Array(0...3) returns an array of numbers [0, 1, 2, 3] generated by the interval.

2. Set initialization

2.1 Creating an empty set

A set is an unordered collection of unique elements. A set in Swift is declared using one form: Set<ElementType>.

To create an empty set, simply apply a pair of parenthesis to its type declaration Set<ElementType>(). Let's define an empty set:

The literal [] is inferred as an empty set of strings based on the parameter type collection: Set<String>.

2.2 Creating a set with values

The array literal for sets

To initialize a set with predefined list of unique elements, Swift allows to use the array literal for sets. The initial elements are comma separated and enclosed in square brackets: [element1, element2, ..., elementN].
It is important that to indicate explicitly the variable type :Set, otherwise the literal by default is creating an array.

[2, 3, 5, 7, 11] is an array literal that creates a set of Int with 5 elements. Notice the presence of type annotation let primeNumbers: Set, which denotes a set creation, but not an array.

Alternatively, the set literal initialization let primeNumbers: Set<Int> = ... can use a longer form of type annotation :Set<Int> (<Int> explicitly indicates the type of set element). However <Int> part can be skipped, since Swift can determine set's element type based on the elements enumerated in the literal (2, 3, ..., 11 are integers).

The set can contain only unique elements. If you accidentally provide duplicated elements, Swift simply ignores the duplication:

Dictionary<String, Int>() and [String: Int]() create empty dictionaries. The key type is a string and the value type is an integer.
Swift infers the array type, so the explicit type annotation may not be specified.

The shorthand form [String: Int]() is preferred over the longer form Dictionary<String, Int>().

On invocation countElements(ofDict: [:]) Swift infers the type of dictionary based on the function parameter declaration collection: [String: Int]. So the empty dictionary literal [:] can be used freely.

3.2 Creating a dictionary with key-value pairs

To initialize the dictionary with known pre-defined key-value pairs use the dictionary literal: [key1: value1, key2: value2, ..., keyN: valueN].

[Any] type annotation enables to specify elements of different types in the literal.
The array elements are of type Any. So you have to convert an array element to an expected type: let str = mixes[0] as? String.
Of course defining such arrays is not recommended.

You may want to create an array of instances of derived classes that inherit from the same parent class. In such situation Swift infers that the type of elements is the parent class (but not any of the derived ones).

Let's define one parent class Bird and two derived Pigeon and Sparrow:

The array literal contains instances of Pigeon and Sparrow that derive from the same parent class Bird. As result Swift infers that birds is an array of type Array<Bird>.

If you need to access the exact Pigeon or Sparrow from the array, use the type casting: let pigeon = bird as? Pigeon or let sparrow = bird as? Sparrow.

5. Conclusion

The collection is one of the most used data structure. The collection declaration and initialization is a common and sometimes tedious task.

To minimize the boilerplate code, Swift provides short and expressive literals to initialize the 3 collection types: arrays, sets and dictionaries.

The array literal [element1, element2, ..., elementN] can be used to initialize arrays and sets (a set requires explicit type annotation :Set).

The dictionary literal [key1: value1, ..., keyN: valueN] initializes dictionaries in a short and comfortable way.

It worth mentioning that Swift's type inference mechanism brings huge benefits. Often you don't even have to indicate the type of a collection, because Swift can infer that information based on the context.

You should take precaution when adding to collection elements of different types. In such case Swift determines the base class or structure of the elements (for example Any, AnyObject or a parent class) and use it to infer the collection type.

Indeed collection initialization in Swift is short and sweet! (Still doubting? Then check this out).

What is your opinion on Swift collection literals? Feel free to write a comment below!