O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

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

Counting the key or values in a dictionary

We use the count property of the dictionary to get the number of key-value pairs in the dictionary. The following example shows how to use this property:

let countries = ["US":"United States", "IN":"India","UK":"United Kingdom"]; 
var cnt = countries.count  //cnt contains 3 

In the preceding code, the cnt variable will contain the number 3 since there are three key-value pairs in the countries dictionary.

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