Extra Bits of Dictionaries

You can create a new dictionary like this:

var vehicles = Dictionary<String,String>()

It is worth noting that once again, if you Command+click on the dictionary, you will see that it is made up of a Struct and subscripts. The <> characters tell you that the dictionary is made with generics, and it will accept any type at all for its keys and values. (Generics are a powerful feature of Swift. and you will learn much more about them in Chapter 9, “Becoming Flexible with Generics.”)

If you want to count the number of key/value pairs in a dictionary, you can use .count:

people.count // 3

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.