From the course: Swift 5 Essential Training

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

Swift dictionaries

Swift dictionaries

- [Instructor] Like arrays, dictionaries are collection types, but instead of holding single values accessed by indexes, they hold key value pairs. Now in these key value pairs, all the keys need to be of the same type and all values need to be of the same type. Now, it's important to know that dictionary items are unordered, so their values are accessed by their associated keys not in sequence. Let's do the same thing we did with arrays and start off with a simple uninitialized dictionary. We're going to call this blacksmithShop and the syntax for a dictionary-type annotation is two brackets. The first type is going to be the type of key that we're going to store separated by a colon, a space, and the value type. So in plain English, we're saying here that blacksmithShop is going to be a dictionary of string keys and int values. To create or initialize an empty dictionary, we use the open and close brackets and a…

Contents